macOS 配置 golang 运行环境

栏目: Go · 发布时间: 4年前

内容简介:存档下 macOS 下配置 golang 的过程。这里我们先要创建一个工作目录,后续所有工作都会在这个目录下展开:配置 $GOPATH 等环境变量,这个操作是必须的,否则 go 命令运行的时候不知道去哪里寻找待执行的文件:

存档下 macOS 下配置 golang 的过程。

第一步,通过 Homebrew 安装 golang 包

$ brew update
$ brew install go
$ go version
go version go1.12.1 darwin/amd64

第二步,配置 golang 工作环境

这里我们先要创建一个工作目录,后续所有工作都会在这个目录下展开:

$ mkdir -p /Users/kodango/Documents/Code/Go

配置 $GOPATH 等环境变量,这个操作是必须的,否则 go 命令运行的时候不知道去哪里寻找待执行的文件:

$ grep GO ~/.bash_profile
export GOPATH=$HOME/Documents/Code/Go    # 上面创建的工作目录
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN

接下来按照官网的建议,在工作目录下创建几个字目录:

$ mkdir -p $GOPATH $GOPATH/src $GOPATH/pkg $GOPATH/bin

第三步,测试是否工作正常

在 $GOPATH/src 目录下创建 hello.go 文件:

kodango -> ~/Documents/Code/Go/src/github.com/kodango/hello
$ ls -l
total 8
-rw-r--r--  1 kodango  staff  86  5 18 09:26 hello.go

写一个简单的 Hello world 示例代码:

package main

import (
    "fmt"
)

func main() {
    fmt.Println("Hello, world\n")
}

使用 go run 命令运行代码:

kodango -> ~/Documents/Code/Go/src/github.com/kodango/hello
$ go run hello.go
Hello, world

或者先用 go install 将编译好的执行文件安装到 $GOBIN 目录。因为之前已经把 $GOBIN 添加到系统 $PATH,可以直接输入 hello 运行。

kodango -> ~/Documents/Code/Go/src/github.com/kodango/hello
$ go install hello.go
$ ls -l "$GOBIN"
total 4120
-rwxr-xr-x  1 kodango  staff  2108040  5 18 09:30 hello
$ hello
Hello, world

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Hatching Twitter

Hatching Twitter

Nick Bilton / Portfolio Hardcover / 2013-11-5 / USD 28.95

The dramatic, unlikely story behind the founding of Twitter, by New York Times bestselling author and Vanity Fair special correspondent The San Francisco-based technology company Twitter has become......一起来看看 《Hatching Twitter》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具