duanlan4801 2015-02-01 05:53
浏览 2
已采纳

将主包放入两个文件中[重复]

This question already has an answer here:

In $GOPATH/src/testapp I have two files

  • main.go
  • otherfile.go

main.go has

package main

import "fmt"

func main() {
  fmt.Println(SomeFunc())
}

otherfile.go has

package main

func SomeFunc() string {
  return "a thing"
}

When I try to run with go run main.go, I get an error

./main.go:6: undefined: SomeFunc

I thought I could create another file with package main and code in main.go would have access to its declarations. Why doesn't this work? How would I do something like this?

</div>
  • 写回答

1条回答 默认 最新

  • duanfan8360 2015-02-01 06:06
    关注

    If I run go run *.go it works.
    It also works if I go build and then ./testapp.
    Hopefully this question helps someone in the future.

    Some comments on this page helped me: https://github.com/go-lang-plugin-org/go-lang-idea-plugin/issues/555

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?