【学习笔记】Golang起步之HelloWord

golang 安装

进入官网--> https://golang.org/

选择适合自己的系统下载

image-20200302220940596

环境变量

  • 添加环境变量

以mac终端为 zsh为例,输入下面命令

>> vi ~/.zshrc

在最后追加下面代码

# /Users/quaint/study/golang-study 为你的golang 工作空间, 该空间下应该包含3个文件夹 src pkg bin
# root 为go安装目录 path 为go工作空间
export GOROOT=/usr/local/go
export GOPATH=/Users/quaint/study/golang-study
export PATH=$PATH:$GOROOT
export PATH=$PATH:$GOPATH:$GOPATH/bin
  • 检查环境变量

重启终端,输入以下命令

>> go version
go version go1.14 darwin/amd64
>> go evn

查看 GOPATH 和 GOROOT 是否指向你的工作空间

vscode 插件安装

  • chinese中文简体(Microsoft)

  • go (Microsoft)

go tools 安装

Mac平台按Command+Shift+P,这个时候VS Code界面会弹出一个输入框,输入以下命令。

Go:Install/Update Tools

选择全部,点击确定or回车

go.toolsGopath setting is not set. Using GOPATH /Users/quaint/study/golang-study
Installing 17 tools at /Users/quaint/study/golang-study/bin in module mode.
  gocode
  gopkgs
  go-outline
  go-symbols
  guru
  gorename
  gotests
  gomodifytags
  impl
  fillstruct
  goplay
  godoctor
  dlv
  gocode-gomod
  godef
  goreturns
  golint

Installing github.com/uudashr/gopkgs/v2/cmd/gopkgs FAILED
Installing github.com/mdempsky/gocode FAILED
Installing github.com/ramya-rao-a/go-outline FAILED
Installing github.com/uudashr/gopkgs/v2/cmd/gopkgs FAILED
Installing github.com/acroca/go-symbols FAILED
Installing github.com/ramya-rao-a/go-outline FAILED
Installing golang.org/x/tools/cmd/guru FAILED
Installing github.com/acroca/go-symbols FAILED
Installing golang.org/x/tools/cmd/gorename FAILED
Installing golang.org/x/tools/cmd/guru FAILED

你会发现好多都安装失败了。可以使用git工具进行安装。

第一步:现在自己的GOPATHsrc目录下创建golang.org/x目录

第二步:在终端中cdGOPATH/src/golang.org/x`目录下

第三步:执行git clone https://github.com/golang/tools.git tools命令

第四步:执行git clone https://github.com/golang/lint.git命令

第五步:按下Ctrl/Command+Shift+P再次执行Go:Install/Update Tools命令,在弹出的窗口全选并点击确定,这一次的安装都会SUCCESSED了。

helloword编写

package main

import "fmt"

func main(){
    fmt.Println("hello world!");
}

helloword编译

# 编译当前工作目录的go文件
>> go build
>> go build fileName
>> go build -o outName.exe

# 成功之后项目会多一个hello word 文件,执行他
>> ./helloword 
hello word!

致谢

感谢李文周的博客详细的Go语言教程。

猜你喜欢

转载自www.cnblogs.com/quaint/p/12405338.html