单个 go 模块中的多个包

Multiple package in a single go module

我是 GO 的新手。试图了解如何在 go 模块中构建我的项目。

如您在屏幕截图中所见,我有一个 go 模块。 我里面有 main.go 。以下为内容

package main

import "go-test/repo/test"

func main() {
    test.GetFun()
}

里面repo/test.go,内容如下

package repo

import "fmt"

// GetFun just for fun
func GetFun() {
    fmt.Println("fun")
}

当我 运行 时,go build 以下是我得到的错误。

   maing.go:3:8: package go-test/repo/test is not in GOROOT (/usr/local/go/src/go-test/repo/test)

Your test.go defines the package repo, so you should import it like import "go-test/repo", and in main refer to it as repo, not as test, like repo.GetFun(). Please read How to Write Go Code.

– icza