Mac下golang开发环境配置

2019-08-07  本文已影响0人  账房先生2016

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

猜你喜欢

热点阅读