34

go 语言学习(1)

 5 years ago
source link: https://studygolang.com/articles/13877?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

安装

77NJfuQ.png!web

image

按照你的系统下载对应的安装包即可

设置环境变量

# 我是 mac 系统默认是装在/usr/local 下的,也可以自己选择想要安装的路径
# windows用户自行百度如何修改环境变量,这里不多做介绍
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
export GOPATH=/Users/yejunyu/go/goget:/Users/yejunyu/go/gowork

这里 gopath 说一下, 每个 go 的目录都有

  • src 存放源代码(比如:.go .c .h .s等)
  • pkg 编译时生成的中间文件(比如:.a)
  • bin 编译后生成的可执行文件
    goroot 即谷歌官方的标准库和执行文件存放的地方
    gopath 是自行设定的存放源码的地方
    我这里用了两个 gopath , 用 : 隔开(windows 用户用 ; 隔开)
    第一个用来存放第三方库(go get获得的库),第二个可以放你自己写的源码

验证

go version

jYZn2aI.png!web

image

说明已经可以使用了

package main

func main(){
    println("hello world")
}

国际惯例,输出一个 hello world
下一节正式开始 golang 之旅, lang 起来吧~


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK