golang的hello-world以及non-main package的trouble-shooting

2022年 1月 8日 41点热度 0人点赞

原文地址:http://blog.duhbb.com/2022/01/08/golang-hello-world-and-non-main-package-trouble-shooting/

我的个人博客地址:http://blog.duhbb.com/

Goland下创建项目:

在Goland中执行这个方法报错了:

Error: Run after the build is not possible.
The 'main' file has the non-main package or does not contain the 'main' function

报错的意思是说,找不到你写的包并没有main函数。 其实呢,package不是IDE自动弹出的包,而是跟go文件名一样的。

  • GOROOT:Go 语言安装根目录的路径,也就是 GO 语言的安装路径。
  • GOPATH:若干工作区目录的路径,是我们自己定义的工作空间。
  • GOBIN:GO 程序生成的可执行文件(executable file)的路径。

不要把GOPATH设置成go的安装路径。 GOPATH是作为编译后二进制的存放目的地和import包时的搜索路径 (其实也是你的工作目录, 你可以在src下创建你自己的go源文件, 然后开始工作)。

$GOPATH目录约定有三个子目录

  • src存放源代码(比如:.go .c .h .s等) 按照golang默认约定,go run,go install等命令的当前工作路径(即在此路径下执行上述命令)。
  • pkg编译时生成的中间文件(比如:.a)  golang编译包时
  • bin编译后生成的可执行文件(为了方便,可以把此目录加入到 $PATH 变量中,如果有多个gopath,那么使用${GOPATH//://bin:}/bin添加所有的bin目录)

关于go的整体一个开发目录

go_project     // go_project为GOPATH目录
  -- bin
     -- myApp1  // 编译生成
     -- myApp2  // 编译生成
     -- myApp3  // 编译生成
  -- pkg
  -- src
     -- myApp1     // project1
        -- models
        -- controllers
        -- others
        -- main.go 
     -- myApp2     // project2
        -- models
        -- controllers
        -- others
        -- main.go 
     -- myApp3     // project3
        -- models
        -- controllers
        -- others
        -- main.go

Linux下配置go环境

# 1. 首先下载linux下的go包:https://studygolang.com/dl/golang/go1.9.2.linux-amd64.tar.gz
# 2. 下载之后解压源码包

tar -zxvf go1.9.2.linux-amd64.tar.gz

# 3. 移动到 /usr/local/go 也就是GOROOT

# 4. 设置GOPATH,还有PATH环境变量

export GOROOT=/usr/local/go #设置为go安装的路径
export GOPATH=$HOME/gocode #默认安装包的路径
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

原文地址:http://blog.duhbb.com/2022/01/08/golang-hello-world-and-non-main-package-trouble-shooting/

我的个人博客地址:http://blog.duhbb.com/

参考

rainbow

这个人很懒,什么都没留下

文章评论