如果已经有K8S环境的同学可以跳过,如果没有,推荐你看看我的《Ubuntu22加Minikue搭建K8S环境》,课程目录如下:
下载:https://code.visualstudio.com/Download
安装命令:
sudo dpkg -i code_1.85.1-1702462158_amd64.deb
将其上传到Downloads目录下。
解压:
tar -xzvf go1.21.5.linux-amd64.tar.gz
将其移动到特定目录:
mv go /usr/local/go1.21
配置环境变量:
vim ~/.bashrc
# 添加如下配置
export GOROOT=/usr/local/go1.21
export GOPATH=/home/zdp/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
export PATH=$PATH:$GOPATH/bin
export GOPROXY=https://goproxy.io,direct
激活环境变量并测试:
source ~/.bashrc
go version
main.go
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
http.HandleFunc("/", handleIndex)
log.Fatal(http.ListenAndServe(":18888", nil))
}
func handleIndex(writer http.ResponseWriter, request *http.Request) {
fmt.Fprintln(writer, "你好,张大鹏")
}
运行:
go run main.go
浏览器访问:http://localhost:18888/