美文网首页
Mac下golang开发环境配置

Mac下golang开发环境配置

作者: 账房先生2016 | 来源:发表于2019-08-07 14:26 被阅读0次

下载安装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

相关文章

网友评论

      本文标题:Mac下golang开发环境配置

      本文链接:https://www.haomeiwen.com/subject/krfwdctx.html