dongqie7806 2014-06-19 22:15
浏览 148
已采纳

GO:另一个文件(相同的程序包)中的对象引发未定义的错误

File: $GOPATH/src/scratch_go_code/main/main.go

package main

import "fmt"

func main() {
    fmt.Println("Hello World")
    cloud := Cloud{}
    cloud.Say()
}

file $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")
}

Running: go install scratch_go_code/... && go run main/main.go throws:

# command-line-arguments
main/main.go:7: undefined: Cloud

Any idea why?

  • 写回答

2条回答 默认 最新

  • duanpao4522 2014-06-19 23:53
    关注

    This should work

    go build scratch_go_code/main
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?