LoginSignup
20
18

More than 5 years have passed since last update.

Golang環境構築

Last updated at Posted at 2017-05-20

概要

golangの実行環境構築と、hello, worldをやってみる。

環境

  • CentOS 7.1
  • go 1.8.1

Goをインストール

$ cd /usr/local/src
$ sudo curl -O https://storage.googleapis.com/golang/go1.8.1.linux-amd64.tar.gz
$ sudo tar -C /usr/local -xzf go1.8.1.linux-amd64.tar.gz

PATHを通す

GOPATHは、ソースを設置する場所なので、適宜変更してください。

~/.bash_profile
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:/usr/local/go/bin

hello, world

こちらを参考にやってみる。
https://golang.org/doc/install#testing

$ mkdir -p $HOME/go/src/hello
$ vi $HOME/go/src/hello/hello.go
hello.go
package main

import "fmt"

func main() {
    fmt.Printf("hello, world\n")
}

buildする。

$ cd $HOME/go/src/hello
$ go build

実行する。

$ ./hello
hello, world

パッケージ管理

パッケージ管理ツールは色々あるが、depが一番よいと思う。

depのインストール

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

ディレクトリ作成

$ mkdir -p $GOPATH/src/prj_root

init

srcというディレクトリがないとエラーになる。

$ cd $GOPATH/src/prj_root
$ dep init
$ ls -l
total 8
-rw-r--r-- 1 root root 222 Sep  7 02:21 Gopkg.lock
-rw-r--r-- 1 root root 655 Sep  7 02:21 Gopkg.toml
drwxr-xr-x 2 root root   6 Sep  7 02:21 vendor

moduleインストール

goのスクリプトを作成し、以下のコマンドを打つと、スクリプトを解析して、未インストールのパッケージを自動でインストールしてくれる。

$ dep ensure

Tutorialなど

参考

20
18
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
20
18