1 基础知识
1.1 简介
- Go是一种开放源代码编程语言
- Go可轻松构建简单、可靠且高效的软件
1.2 Go语言的特点
- Go富有表现力、简介、整洁且高效
- Go的并发机制使编写程序可轻松地从多核和联网的机器中获得最大收益
- Go的新颖类型系统可构建灵活的模块化程序
- Go可以快速编译成机器码且具有便利的垃圾回收和运行时反射功能
- Go是一种快熟、静态类型的编程语言,但有动态类型解析语言的效果
2 最佳实践
2.1 部署前的准备
2.1.1 熟悉Go语言的安装
本章节需要你先掌握Linux系统的Go语言二进制的部署,如你尚未具备此知识,烦请参阅如下章节熟悉,
2.1.2 部署Docker环境
本章使用如下Docker环境部署,参阅本章建议你先搭建以下Docker集群环境并熟悉,
2.1.2 准备系统镜像
In docker01
cd /data/docker/images/ docker load -i centos_centos7.3.1611.tar docker tag centos:centos7.3.1611 docker01.cmdschool.org:5000/centos:centos7.3.1611 docker push docker01.cmdschool.org:5000/centos:centos7.3.1611 docker image rm centos:centos7.3.1611
另外,安装包的离线下载请在能上网的docker环境的机器上使用如下命令,
docker pull centos:centos7.3.1611 docker save centos:centos7.3.1611 -o centos_centos7.3.1611.tar
另外,其他版本请从以下链接下载,
https://hub.docker.com/_/centos
2.1.3 下载Go语言的二进制安装包
cd ~ wget https://dl.google.com/go/go1.14.4.linux-amd64.tar.gz
注,另外其他版本请从以下链接下载,
https://golang.org/dl/
2.2 创建Go语言镜像
2.2.1 创建镜像项目文件夹
cd ~ mkdir ~/imageProject-go-1.14.4
2.2.2 创建Dockerfile
vim ~/imageProject-go-1.14.4/Dockerfile
加入如下配置,
FROM docker01.cmdschool.org:5000/centos:centos7.3.1611 MAINTAINER will@cmdschool.org # Deploy Go Language COPY go1.14.4.linux-amd64.tar.gz /root WORKDIR /root RUN tar -C /usr/local/ -xf go1.14.4.linux-amd64.tar.gz ENV GOLAN_HOME=/usr/local/go ENV PATH=${GOLAN_HOME}/bin:$PATH RUN rm -r /root/go1.14.4.linux-amd64.tar.gz
“FROM”指令声明基于“centos7.3.1611”镜像打包
“COPY”指令声明复制当前目录的具体文件到打包的镜像
“ENV”指令声明JAVA JDK或Tigase所需的环境变量
“RUN”指令声明容器环境执行的命令
“WORKDIR”指令声明切换容器内部的某个目录
所以我们需要根据定义的文件准备需要复制的文件,
cd ~ cp go1.14.4.linux-amd64.tar.gz imageProject-go-1.14.4/
确认所需的文件存在,
ls ~/imageProject-go-1.14.4/
可见如下显示,
Dockerfile go1.14.4.linux-amd64.tar.gz
2.2.3 执行打包操作
cd ~/imageProject-go-1.14.4 docker build -t go:1.14.4 .
以上“.”指当前目录为编译目录,编译程序会自动加载“Dockerfile”文件定义,可见如下显示,
Sending build context to Docker daemon 123.7MB Step 1/8 : FROM docker01.cmdschool.org:5000/centos:centos7.3.1611 ---> c5d48e81b986 [...] Successfully built 1c471b2081c1 Successfully tagged go:1.14.4
如果镜像集群公用,还需要执行以下命令,
docker tag go:1.14.4 docker01.cmdschool.org:5000/go:1.14.4 docker push docker01.cmdschool.org:5000/go:1.14.4 docker image rm go:1.14.4
完成后,可使用如下命令查看镜像,
docker images
可见如下显示,
REPOSITORY TAG IMAGE ID CREATED SIZE docker01.cmdschool.org:5000/go 1.14.4 1c471b2081c1 29 seconds ago 650MB [...]
2.2.4 测试软件运行
docker run -d --name go docker01.cmdschool.org:5000/go:1.14.4 /usr/sbin/init
以上运行容器环境后,我们使用以下命令登录容器虚拟机,
docker exec -it `docker container ls | grep 'go:1.14.4' | cut -d" " -f1 ` /bin/bash
然后,可使用如下命令确认环境变量生效,
go version
可见如下显示,
go version go1.14.4 linux/amd64
如果需要取得帮助,可使用如下命令,
go help
可见如下显示,
Go is a tool for managing Go source code. Usage: go [arguments] The commands are: bug start a bug report build compile packages and dependencies clean remove object files and cached files doc show documentation for package or symbol env print Go environment information fix update packages to use new APIs fmt gofmt (reformat) package sources generate generate Go files by processing source get download and install packages and dependencies install compile and install packages and dependencies list list packages or modules mod module maintenance run compile and run Go program test test packages tool run specified go tool version print Go version vet report likely mistakes in packages Use "go help " for more information about a command. Additional help topics: buildmode build modes c calling between Go and C cache build and test caching environment environment variables filetype file types go.mod the go.mod file gopath GOPATH environment variable gopath-get legacy GOPATH go get goproxy module proxy protocol importpath import path syntax modules modules, module versions, and more module-get module-aware go get module-auth module authentication using go.sum module-private module configuration for non-public modules packages package lists and patterns testflag testing flags testfunc testing functions Use "go help " for more information about that topic.
另外,也可以通过创建代码测试,
cd ~/ vi hello.go
加入如下代码,
package main import "fmt" func main() { fmt.Printf("hello, world\n") }
然后编译代码
cd ~/ go build hello.go
尝试运行编译后的程序,
cd ~/ ./hello
可见如下显示,
hello, world
测试完成后可使用如下命令退出容器虚拟机,
exit
另外,你可以使用如下命令查询当前运行的container,
docker ps -a
可见如下显示,
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2949b668ba24 docker01.cmdschool.org:5000/go:1.14.4 "/usr/sbin/init" 5 minutes ago Up 5 minutes go [...]
然后可以使用如下命令停止并删除,
docker container stop 2949b668ba24 docker container rm 2949b668ba24
参阅文档
====================
没有评论