Mac下golang开发环境配置

下载安装go

$ brew install go

设置GOPATH

$ vim ~/.bash_profile
// add below lines to the file
export GOPATH=$HOME/Documents/GoWorkSpace
export GO111MODULE=on
// Source the new environment
$ source ~/.bash_profile

测试

$ go env

创建一个文件在$GOPATH/src/github.com/YOUR_COMPANY_NAME/YOUR_PROJECT_NAME
例如:Vincent/Documents/GoWorkSpace/src/github.com/Vincent/YOUR_PROJECT_NAME

package main

import (
    "fmt"
    "log"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}

func main() {
    http.Handle("/", handler)
    log.Fatal(http.ListenAndServe(":3060", nil))
}

在该目录下运行:

$ go build
$ ./YOUR_PROJECT_NAME

然后在浏览器中输入http://localhost:3060/test,正常会再浏览器上看到

Hi there, I love test
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容