GO:另一个文件(同一个包)中的对象抛出未定义的错误

时间:2014-06-19 22:15:05

标签: go

文件:$ 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

知道为什么吗?

2 个答案:

答案 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