跳至主要內容

Go语言安装

知识库编程技巧GoLangGO大约 2 分钟

Go 语言被设计成一门应用于搭载 Web 服务器,存储集群或类似用途的巨型中央服务器的系统编程语言。

Go 语言特色

  • 简洁、快速、安全
  • 并行、有趣、开源
  • 内存管理、数组安全、编译迅速

Go 语言用途

对于高性能分布式系统领域而言,Go 语言无疑比大多数其它语言有着更高的开发效率。它提供了海量并行的支持,这对于游戏服务端的开发而言是再好不过了。

下载软件包

下载地址:https://go.dev/dl/open in new window

下载地址:https://golang.google.cn/dl/open in new window

windows: https://go.dev/dl/go1.19.windows-amd64.msiopen in new window

linux:https://go.dev/dl/go1.19.linux-amd64.tar.gzopen in new window

macOS:https://go.dev/dl/go1.19.darwin-amd64.pkgopen in new window

imgopen in new window
img

设置环境变量

  • windows

    setx /m GO_HOME D:\windows\software\Go

    %GO_HOME%/bin配置到环境变量PATH

    go version

  • linux
cat >> /etc/profile <<EOF
export GO_HOME=/usr/local/lib/go
export GOPATH=\$HOME/go
export PATH=\$PATH:\$GO_HOME/bin:\$GOPATH/bin
EOF
# 配置生效
source /etc/profile
## 查看go版本
go version

测试程序

cat >> hello.go <<EOF
package main

import (
 "fmt"
 "os"
)

func main() {
 fmt.Println("Hello world!")

 dir, _ := os.Executable()
 fmt.Println(dir)
}
EOF
## 运行程序
go run hello.go
## 在当前目录下生成hello.exe 执行hello.exe即可
go build hello.go
## 在全局目录下生成hello.exe 可任何目录执行hello.exe
go install hello.go

补充

windows 下建议cmder来替代cmd

Go 编码开发工具:GolandlietIDEVisual Studio Code + Go插件IntelliJ + Go 插件

常用工具:https://github.com/golang/tools.githttps://github.com/golang/lint.git

格式化输出

package main
import "fmt"
func main() {
  fmt.Printf("            占位输出:%v, %+v, %#v \n", 123, 123, 123)
  fmt.Printf("            类型输出:%T \n", 123)

  fmt.Printf("          二进制输出:%b \n", 123)
  fmt.Printf("          八进制输出:%o \n", 123)
  fmt.Printf("          十进制输出:%d \n", 123)
  fmt.Printf("       5位十进制输出:%5d \n", 123)
  fmt.Printf("5位十进制输出(补0):%05d \n", 123)
  fmt.Printf("    十六进制小写输出:%x \n", 123)
  fmt.Printf("    十六进制大写输出:%X \n", 123)

  fmt.Printf("        unicode 码值:%c \n", 97)
  fmt.Printf("            转义输出:%q \n", 97)
  fmt.Printf("     unicode格式输出:%U \n", '一')

  fmt.Printf("无小数部分、二进制指数的科学计数法:%b \n", 123.123456)
  fmt.Printf("                     有2位小数部分:%.2f \n", 123.125456)
  fmt.Printf("         有6位小数部分的科学计数法:%.6e \n", 123.123456)
  fmt.Printf("                        科学计数法:%E \n", 123.123456)
}

godoc 安装

go env
## 设置env
go env -w GO111MODULE=auto
go env -w GOPROXY=https://goproxy.io,direct
go env -w GOPROXY=https://goproxy.cn,direct
go env -w GOPROXY=https://proxy.golang.org,direct
## 安装
go get golang.org/x/tools/cmd/godoc
## 访问`http://localhost:8080`即可
godoc -http=:8080