go-modules

Organize local code in packages using Go modules

混江龙づ霸主 提交于 2019-11-27 01:47:05
I can not find a way to factor out some code from main.go into a local package when using Go modules (go version >= 1.11) outside of $GOPATH . I am not importing any external dependencies that need to be included into go.mod , I am just trying to organize locally the source code of this Go module. The file main.go : package main // this import does not work import "./stuff" func main() { stuff.PrintBaz() } The file ./stuff/bar.go (pretending to be a local package): package stuff import "log" type Bar struct { Baz int } func PrintBaz() { baz := Bar{42} log.Printf("Bar struct: %v", baz) } The

Organize local code in packages using Go modules

天大地大妈咪最大 提交于 2019-11-26 09:09:39
问题 I can not find a way to factor out some code from main.go into a local package when using Go modules (go version >= 1.11) outside of $GOPATH . I am not importing any external dependencies that need to be included into go.mod , I am just trying to organize locally the source code of this Go module. The file main.go : package main // this import does not work import \"./stuff\" func main() { stuff.PrintBaz() } The file ./stuff/bar.go (pretending to be a local package): package stuff import \

How to use a module that is outside of “GOPATH” in another module?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 08:56:48
问题 I\'ve created a library as the module for personal use outside of \"GOPATH\" in \"database\" folder with this command \"go mod init database,\" and I don\'t know: How to use/ import this module in another module? OS: Windows 7 , Go: v1.11 回答1: The easiest and working out-of-the-box solution is to put your database package / module into a VCS (e.g. github.com), so other packages (inside other modules) can simply refer to it by importing it like: import "github.com/someone/database" If you do