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

时间:2024-10-05 15:48:46

原文: /doc/#PackagePaths

 

 

 

 

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

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

不同的话,是会报错的。

 

package main

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

func say() {
    ("say function call!")
}

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

package main2

import (
    "fmt"
)

func fly() {

    ("adada")
}

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

 

You cannot have two packages per directory, hence the error. So the solution as @Larry Battle said to move your  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.