原文: https://golang.org/doc/code.html#PackagePaths

 go同一个目录下的go文件里面不能有多个package

 

go同一个目录下的go文件里面不能有多个package

 go同一个目录下的go文件里面不能有多个package

 

-------------------------------------------------------------------------------------------------------------------------------------

如果demo目录下有两个文件 main.go 和mian2.go的话,main.go 和main2.go文件中的package 定义的名字要是同一个

不同的话,是会报错的。

 

package main

import "fmt"
import (
// "../demo/f1"
// "./f1"
)

func say() {
    fmt.Println("say function call!")
}

func main() {
    fmt.Println("hello, world")
    say()
    fly()
    // f1.F1()
    // f1.F2()
}

main2.go

package main2

import (
    "fmt"
)

func fly() {

    fmt.Println("adada")
}

go同一个目录下的go文件里面不能有多个package

main2.go 中的package main2 改为 main就是可以的。

go同一个目录下的go文件里面不能有多个package

 

You cannot have two packages per directory, hence the error. So the solution as @Larry Battle said to move your myproject.go to a new directory.

From How to write go code

Go code must be kept inside a workspace. A workspace is a directory hierarchy with three directories at its root:

src contains Go source files organized into packages (one package per directory),

pkg contains package objects, and

bin contains executable commands.

 go同一个目录下的go文件里面不能有多个package

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-15
  • 2022-12-23
  • 2021-06-15
  • 2021-06-27
  • 2022-01-16
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-09-14
  • 2022-12-23
  • 2021-10-03
  • 2021-11-05
  • 2021-11-30
相关资源
相似解决方案