1、安装Go和对应编辑器
go官网,下载自己电脑对应版本即可。安装完成之后打开cmd输入go即可弹出对应提示。
对于编辑器部分,我这里不使用GoLand,使用的是vscode。你们随意
vsCode中我使用了以下插件:
2、编写helloWord
利用VsCode创建一个main.go文件,之后可以编写主函数:
package main
func main() {
println("Hello world!")
}
之后可以直接go run main.go就能运行起来单个文件了。
3、项目目录开发
和python一样,go的开发除了单文件开发,还有包开发的方式。它的包开发中对于外部依赖的管理主要是通过.mod文件进行描述的,类似于java中maven的pom描述文件。我们可以通过以下方式创建它:
go mod init demo
4、编写一个http服务器
go的原生net库就支持http端口监听:
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
http.HandleFunc("/hello", func(writer http.ResponseWriter, request *http.Request) {
params := request.URL.Query()
fmt.Fprintf(writer, "你好, %s", params.Get(