dongqu9917 2019-08-03 12:40
浏览 115
已采纳

是否可以从模块中将〜/ go / src /导入本地包? [关闭]

I am using go module, the file structure is like this:

~/some_path/goapp/go.mod
~/some_path/goapp/go.sum
~/some_path/goapp/main.go

~/go/src/fakedomain.com/fakeuser/foo/foo.go

Inside main.go, I tried to do

import fakedomain.com/fakeuser/foo

But, it doesn't work at all. I tried to add the following to go.mod:

require fakedomain.com/fakeuser/foo 

OR

require fakedomain.com/fakeuser/foo
replace fakedomain.com/fakeuser/foo /home/user/go/src/fakedomain.com/fakeuser/foo

None of them works. How can I achieve this?

Edited

This question is about how to import a local package inside ~/go/src/ from a module which is outside ~/go/src/.

In other words, the module which is outside ~/go/src/ will import a local package inside ~/go/src/. I thought I could import it directly (that is what I did in the old days without module), but I was wrong. It turns out that I have to make the local package inside ~/go/src/ become a module too.

  • 写回答

2条回答 默认 最新

  • dongzeao5047 2019-08-03 13:40
    关注

    Thank @MartinTournoij, @Peter, @DaveC very much for their help and comments which I have upvoted. After following all the directions, I finally make it work.

    (Btw, I really shouldn't trust VSCode error message too much. Because I normally check errors from VSCode. Thus I didn't try go build before asking this question. I thought they should return the same error, but they are not. go build provides more reasonable error messages than VSCode.)

    There were three problems.

    • Missing version from require in go.mod
    • Missing => from replace in go.mod
    • Missing go.mod for foo package.

    So to make it work:

    File Structure:

    ~/some_path/goapp/go.mod
    ~/some_path/goapp/go.sum
    ~/some_path/goapp/main.go
    
    ~/go/src/fakedomain.com/fakeuser/foo/foo.go
    ~/go/src/fakedomain.com/fakeuser/foo/go.mod
    

    ~/some_path/goapp/go.mod:

    ...
    require fakedomain.com/fakeuser/foo v0.0.0
    replace fakedomain.com/fakeuser/foo => /home/user/go/src/fakedomain.com/fakeuser/foo
    

    main.go:

    package main
    import fakedomain.com/fakeuser/foo
    ...
    

    ~/go/src/fakedomain.com/fakeuser/foo/go.mod:

    module fakedomain.com/fakeuser/foo
    
    go 1.12
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序 用oss下载 aliyun-oss-sdk-6.18.0.min client报错
  • ¥15 ArcGIS批量裁剪
  • ¥15 labview程序设计
  • ¥15 为什么在配置Linux系统的时候执行脚本总是出现E: Failed to fetch http:L/cn.archive.ubuntu.com
  • ¥15 Cloudreve保存用户组存储空间大小时报错
  • ¥15 伪标签为什么不能作为弱监督语义分割的结果?
  • ¥15 编一个判断一个区间范围内的数字的个位数的立方和是否等于其本身的程序在输入第1组数据后卡住了(语言-c语言)
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥15 抖音看过的视频,缓存在哪个文件