LoginSignup
7
8

More than 5 years have passed since last update.

Goの環境設定 in AWS Cloud9

Last updated at Posted at 2018-11-21

環境

  • AWS Cloud9(シンガポールリージョンのEC2上で動作)
  • Amazon Linux2
  • bash

やること

  1. goenvで任意のバージョンのgoをインストール
  2. GOPATHの設定
  3. 最新安定版のgo1.11.2(2018/11/21現在 https://golang.org/dl/ )をインストール
  4. depのインストール

手順

通常のLinux環境での手順と変わらない

まずはLinux環境のアップデートを行う

Amazon Linux2はRHEL系なのでyum


$ sudo yum -y update

goenvの設定

goenvとは

goのバージョン管理ツールで、任意のバージョンのgoを環境にインストールし、使い分けることができるようになる
たとえば、プロジェクトごとに異なるバージョンのgoを使用する際にとても便利
goenvと同様に、pythonにはpyenv, rubyにはrbenvがある

goenvのインストール

goenvは複数ある(作っている人が違うものが複数ある)が、今回は下記のリポジトリからインストールする
syndbg/goenv


$ git clone https://github.com/syndbg/goenv.git ~/.goenv

パスを通す


# ~/.bashrcに追記する
export GOENV_ROOT=$HOME/.goenv
export PATH=$GOENV_ROOT/bin:$PATH
eval "$(goenv init -)"

設定を読み込ませる


$ source ~/.bashrc

テスト


$ goenv -v
goenv 1.23.0

バージョンが表示されればOK

goのインストール

インストールできるバージョンリストを確認する


$ goenv install -l
Available versions:
  1.2.2
  1.3.0
  1.3.1
  ...
  1.11.2

go1.11.2 をインストール


$ goenv install 1.11.2

使用できるgoのバージョンを確認

Amazon Linuxにはもともとgoがインストールされているのでプリインストールのgoと1.11.2の両方が使えるようになっているはず


$ goenv versions
* system (set by /home/ec2-user/.goenv/version)
  1.11.2

使用するバージョンを1.11.2にする


$ goenv global 1.11.2
$ goenv rehash

テスト

goのバージョンを確認してgo1.11.2になっていればOK


$ go version
go1.11.2 linux/amd64

GOPATHの設定

GOPATHを~/.bashrcにて設定する


# ~/.bashrcに追記する
export GOPATH=$HOME/environment/go
PATH=$PATH:$GOPATH/bin

ディレクトリの作成


$ mkdir -p $GOPATH/src/
$ cd $GOPATH/src/
# これでディレクトリに移動できればOK!

depのインストール

depとは、go公式のパッケージマネジャー

インストール


$ go get -u github.com/golang/dep/cmd/dep

テスト

depのヘルプが表示されればOK!

$ dep help
Dep is a tool for managing dependencies for Go projects

Usage: "dep [command]"

Commands:

  init     Set up a new Go project, or migrate an existing one
  status   Report the status of the project's dependencies
  ensure   Ensure a dependency is safely vendored in the project
  version  Show the dep version information
  check    Check if imports, Gopkg.toml, and Gopkg.lock are in sync

Examples:
  dep init                               set up a new project
  dep ensure                             install the project's dependencies
  dep ensure -update                     update the locked versions of all dependencies
  dep ensure -add github.com/pkg/errors  add a dependency to the project

Use "dep help [command]" for more information about a command.

 できるようになったこと

  1. $GOPATH/src以下でgitのプロジェクトを作成してgoアプリケーションの開発ができます
  2. depでgoのパッケージ管理ができるようになります
7
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8