好好爱自己!

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

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

 

 

 

 

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

如果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")
}

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

 

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.

 

 

posted @ 2018-07-20 09:06  立志做一个好的程序员  阅读(3190)  评论(0编辑  收藏  举报

不断学习创作,与自己快乐相处