• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

原文: 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.

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Go实现的一个命令行HTTP抓包工具发布时间:2022-07-10
下一篇:
go语言path包和filepath包的学习与使用发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap