2019-09-11 17:26
浏览 58


I am creating 3 separate go projects: ace, aces-client and a library shared by both projects aceslib. I am having trouble including the shared library according to the go-documentation (

all go files in aceslib share the package-name aceslib. I am including the library in ace and aces-client with import lib "aceslib". I can build the library with go build and it gets installed with go install, in the directory listing one can see that the file go/pkg/windows_amd64/aceslib.a gets created.

But when I try to build ace or ace-client go complains:

$ go build 
build ace: cannot load aceslib: cannot find module providing package aceslib

My go setup:

$ go version
go version go1.12.9 windows/amd64
$ go env
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Yulivee\AppData\Local\go-build
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\Yulivee\go
set GOROOT=c:\go
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set GOMOD=C:\Users\Yulivee\go\src\ace\go.mod
set CGO_CFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\Yulivee\AppData\Local\Temp\go-build024649150=/tmp/go-build -gno-record-gcc-switches

Directory Structure:

├── bin
│   ├── ace-client.exe
│   └── ace.exe
├── pkg
│   ├── mod
│   │   ├── cache
│   │   └──
│   └── windows_amd64
│       ├── ace
│       ├── aceslib.a
│       └──
└── src
    ├── ace
    │   ├──
    │   ├── go.mod
    │   ├── go.sum
    │   └── main.go
    ├── ace-client
    │   └── main.go
    ├── aceslib
    │   ├──
    │   ├── crypto.go
    │   ├── go.mod
    │   └── utils.go

What am I missing?

Things I have tried, that did not help and lead to the same error:

$ go clean -i -x -cache -modcache
$ chmod 755 go/pkg/windows_amd64/aceslib.a
$ go get
build ace: cannot load aceslib: cannot find module providing package aceslib
$ rm -rf go/pkg/*
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douhuangjian9627
    douhuangjian9627 2019-09-11 17:52

    The directory structure and package names are a bit off - it should be a URL to your project's repo, like every other import that isn't from the stdlib. If you have no repo and want to use local references you'll need to add a replace directive to ace/go.mod like so:

    replace aceslib => ../aceslib

    As documented in the Modules docs.

    点赞 评论
  • dongxia2030
    dongxia2030 2019-09-11 18:13

    IIRC, package names without directories are reserved for stdlib packages. So, you should first push the source tree down at least a level so you can access it by "dir/package".

    Next, do you really want to treat these packages as separate modules? You have separate mod files under ace and aceslib packages. You may combine them under one module with a single go.mod at the project root and make life easier for yourself. That way you don't need redirects, etc.

    You also have to include the modules with their directories:

    import lib "dir/aceslib"
    点赞 评论