文件:$ GOPATH / src / scratch_go_code / main / main.go
package main
import "fmt"
func main() {
fmt.Println("Hello World")
cloud := Cloud{}
cloud.Say()
}
文件$ GOPATH / src / scratch_go_code / main / cloud.go
package main
import "fmt"
type Cloud struct{}
func (Cloud) Say() {
fmt.Println("I'm a cloud in the main package")
}
正在运行:go install scratch_go_code / ...&&去运行main / main.go 抛出:
# command-line-arguments
main/main.go:7: undefined: Cloud
知道为什么吗?
答案 0 :(得分:3)
您必须使用go build
或将这两个文件传递给go run
,例如:
go run main/*.go
使用go build:
cd scratch_go_code/ && go build && ./scratch_go_code
答案 1 :(得分:3)
这应该有效
go build scratch_go_code/main