doutian3010 2016-08-04 05:34
浏览 410

一个目录中可以有多个Go源文件吗? [重复]

This question already has an answer here:

I found this to be a duplicate of this question.




Hello World

Just started learning golang and tried to figure out how to structure a larger program. Not sure if packages are the split I want or if there is something else more suitable for having multiple source files in a single directory, but here's what I tried.

package main

import "fmt"
func main() {
    fmt.Printf("hello, world
")
}

Testing running it:

~/b2/go/src/github.com/bemmu/hello bemmu$ go run hello.go
hello, world

Two file version

I wanted to try splitting it into two files.

main.go

package main

import "fmt"
import "say"

func main() {
    say.Hello()
}

say.go

package say
import "fmt"

func Hello() {
    fmt.Printf("hello, Go
")
}

Testing running it:

~/b2/go/src/github.com/bemmu/hello_split bemmu$ go run main.go
main.go:4:8: cannot find package "say" in any of:
    /usr/local/go/src/say (from $GOROOT)
    /Users/bemmu/b2/go/src/say (from $GOPATH)

In the docs there is an example of creating a library and importing it, but in the example case it is put into a separate directory.

</div>
  • 写回答

1条回答 默认 最新

  • douchuose2514 2016-08-04 05:38
    关注

    No, Just create a new directory for another package.

    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探