Windows に Go言語(golang) の開発環境を構築する
Go言語のインストールと IDE の設定までの手順メモ
検証環境
Windows10 Home Edition
Go言語のインストール
ダウンロード
こちらのサイトから Windows版のアーカイブファイルをダウンロードする (2018年3月31日現在、最新バージョンは 1.10.1)
今回はインストーラ(msi)を使わず、zip から手動でインストールした
Downloads - The Go Programming Language
インストール
ダウンロードした zip を適当なディレクトリに解凍する (以下、D:\prog\go に解凍したものとして説明する)
環境変数
"システムのプロパティ" → "詳細設定" → "環境変数" で GOROOT、GOPATH、PATH の環境変数を設定する
設定の基準は下記の通り
GOROOT
Go言語のインストール先のパス (今回は D:\prog\go)
GOPATH
go get で取得するパッケージやプロジェクトの配置先 (例、D:\home\takaya030\go)
PATH
%GOROOT%\bin と %GOPATH%\bin を PATH に追加する
動作確認
go version や go help が動作するか確認する
D:\>go version go version go1.10.1 windows/amd64 D:\>go help Go is a tool for managing Go source code. Usage: go command [arguments] The commands are: build compile packages and dependencies clean remove object files and cached files doc show documentation for package or symbol env print Go environment information bug start a bug report fix update packages to use new APIs fmt gofmt (reformat) package sources generate generate Go files by processing source get download and install packages and dependencies install compile and install packages and dependencies list list packages run compile and run Go program test test packages tool run specified go tool version print Go version vet report likely mistakes in packages Use "go help [command]" for more information about a command. Additional help topics: c calling between Go and C buildmode build modes cache build and test caching filetype file types gopath GOPATH environment variable environment environment variables importpath import path syntax packages package lists testflag testing flags testfunc testing functions Use "go help [topic]" for more information about that topic.
開発ツールのインストール
D:\>go get -u -v github.com/Masterminds/glide D:\>go get -u -v github.com/nsf/gocode D:\>go get -v -v github.com/uudashr/gopkgs/cmd/gopkgs D:\>go get -u -v github.com/rogpeppe/godef D:\>go get -u -v sourcegraph.com/sqs/goreturns D:\>go get -u -v github.com/golang/lint/golint D:\>go get -u -v github.com/lukehoban/go-find-references D:\>go get -u -v github.com/lukehoban/go-outline D:\>go get -u -v golang.org/x/tools/cmd/gorename D:\>go get -u -v github.com/newhook/go-symbols D:\>go get -u -v golang.org/x/tools/cmd/guru D:\>go get -u -v github.com/derekparker/delve/cmd/dlv
Visual Studio Code のインストール
ダウンロード
こちらのサイトから Windows版のアーカイブファイルをダウンロードする (2018年3月31日現在、最新バージョンは 1.21.1)
今回はインストーラ(exe)を使わず、zip から手動でインストールした
code.visualstudio.com
パスの設定
D:\prog\VSCode-win32-x64-1.21.1\bin を PATH に追加する
Visual Studio Code の Go言語拡張のインストール
"拡張機能" から Go for Visual Studio Code をインストールする
参考サイト
docker-machin upgrade のときにローカルの boot2docker.iso を使う
docker-machine upgrade を実行することで GitHub から最新の boot2docker.iso がダウンロードされ Docker ホスト OS (CoreOS) が更新されるが、あらかじめダウンロード済みの boot2docker.iso を使う場合の手順メモ
検証環境
Windows10 Home Edition VirtualBox 5.2.8 Docker version 18.02.0-ce, build fc4de447b5 docker-machine.exe version 0.13.0, build 9ba6da9
アップグレード前のホストOSの確認
$ docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS dev - virtualbox Running tcp://192.168.99.100:2376 v17.10.0-ce
boot2docker.iso のダウンロード
GitHub から任意のバージョンの iso をダウンロードする
(今回自分がダウンロードのは v17.12.1-ce)
Releases · boot2docker/boot2docker
ホストOSのアップグレード
- ダウンロードした boot2docker.iso を C:\Users\<ユーザー名>\.docker\machine\cache フォルダにコピー
- docker-machine upgrade を実行
$ docker-machine upgrade dev Starting machine so machine can be upgraded... Starting "dev"... (dev) Check network to re-create if needed... (dev) Windows might ask for the permission to configure a dhcp server. Sometimes, such confirmation window is minimized in the taskbar. (dev) Waiting for an IP... Machine "dev" was started. Waiting for SSH to be available... Detecting the provisioner... Waiting for SSH to be available... Detecting the provisioner... Upgrading docker... Stopping machine to do the upgrade... Upgrading machine "dev"... Copying C:\Users\takaya030\.docker\machine\cache\boot2docker.iso to C:\Users\takaya030\.docker\machine\machines\dev\boot2docker.iso... Starting machine back up... (dev) Check network to re-create if needed... (dev) Windows might ask for the permission to configure a dhcp server. Sometimes, such confirmation window is minimized in the taskbar. (dev) Waiting for an IP... Restarting docker...
アップグレードの確認
$ docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS dev * virtualbox Running tcp://192.168.99.100:2376 v17.12.1-ce
参考サイト
MSYS2 の golang 環境に dep をインストール
検証環境
Windows10 Home Edition msys2-x86_64-20170918 $ go version go version go1.9.1 windows/amd64
dep とは
glide に代わる golang のパッケージ管理ツール
github.com
$GOPATH/bin にパスを通す
.bashrc などに下記の設定を追加しておく
export PATH=$GOPATH/bin:$PATH
動作確認
$ 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 prune Pruning is now performed automatically by dep ensure. version Show the dep version information 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.
MSYS2 の Go言語(golang) から SDL2 を使う
検証環境
Windows10 Home Edition msys2-x86_64-20170918 $ go version go version go1.9.1 windows/amd64
MSYS2 に SDL2, Go言語をインストール
下記サイトの手順でインストールする
takaya030.hatenablog.com
takaya030.hatenablog.com
veandco/go-sdl2 のインストール
Go から SDL2 を使うためのラッパーとして veandco/go-sdl2 を使用します
github.com
以下の通り go get でインストールします
後ほど出てくるサンプルコードで go-gl を使用しているので、一緒にインストールします
$ go get -v github.com/veandco/go-sdl2/sdl $ go get -v github.com/veandco/go-sdl2/mix $ go get -v github.com/veandco/go-sdl2/ttf $ go get -v github.com/go-gl/gl/v2.1/gl
動作確認
こちらのサンプルコードを動かしてみます
go-sdl2-examples/opengl.go at master · veandco/go-sdl2-examples · GitHub
$ go build opengl.go $ ./opengl.exe
以下のウィンドウが表示されれば成功です
MSYS2 環境に Go言語(golang) をインストール
検証環境
Windows10 Home Edition msys2-x86_64-20170918
MSYS2 のインストール
下記サイトの手順で 64bit 版をインストールする
takaya030.hatenablog.com
Go言語のインストール
MSYS2 のシェルで下記コマンドでインストールする
go get でパッケージをインストールするときに git を使用するため git もインストールする
$ pacman -S mingw-w64-x86_64-go $ pacman -S git
環境変数の設定
GOROOT は自動で /mingw64/lib/go に設定されるので、GOPATH の設定を .bashrc に追加する
.bashrc
# パスは各自の環境に合わせて適宜変更する export GOPATH=/c/Users/takaya030/msys2/go
動作確認
以下のサンプルコードが動作するか確認する
hello.go
package main import "fmt" func main() { fmt.Printf("hello, world\n") return }
実行
$ go run hello.go hello, world
MSYS2 + MinGW 環境に SDL2 をインストール
検証環境
Windows10 Home Edition msys2-x86_64-20170918 $ gcc --version gcc.exe (Rev2, Built by MSYS2 project) 7.2.0 Copyright (C) 2017 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
MSYS2、MinGW のインストール
下記サイトの手順で 64bit 版をインストールする
takaya030.hatenablog.com
SDL2 のインストール
今回はパッケージマネージャ (pacman) を使ってインストールする
SDL2 パッケージの確認
64bit 環境なので mingw64 のパッケージを使う
$ pacman -Sl | grep SDL2 mingw32 mingw-w64-i686-SDL2 2.0.7-1 mingw32 mingw-w64-i686-SDL2_gfx 1.0.1-2 mingw32 mingw-w64-i686-SDL2_image 2.0.2-1 mingw32 mingw-w64-i686-SDL2_mixer 2.0.2-2 mingw32 mingw-w64-i686-SDL2_net 2.0.1-1 mingw32 mingw-w64-i686-SDL2_ttf 2.0.14-1 mingw64 mingw-w64-x86_64-SDL2 2.0.7-1 mingw64 mingw-w64-x86_64-SDL2_gfx 1.0.1-2 mingw64 mingw-w64-x86_64-SDL2_image 2.0.2-1 mingw64 mingw-w64-x86_64-SDL2_mixer 2.0.2-2 mingw64 mingw-w64-x86_64-SDL2_net 2.0.1-1 mingw64 mingw-w64-x86_64-SDL2_ttf 2.0.14-1
インストール
$ pacman -S mingw-w64-x86_64-SDL2 $ pacman -S mingw-w64-x86_64-SDL2_mixer $ pacman -S mingw-w64-x86_64-SDL2_ttf
動作確認
以下のサンプルコードが動作するか確認する
sdl2_sample.cpp
#include <GL/gl.h> #include <GL/glu.h> #include <SDL2/SDL.h> #include <iostream> #define SDL_WINDOW_TITLE "SDL2" #define SDL_WINDOW_WIDTH (640) #define SDL_WINDOW_HEIGHT (480) #define SDL_PrintError(name) \ do { \ std::cerr << #name << ": " << SDL_GetError() << std::endl; \ } while (0) static SDL_Window *gWindow; static SDL_GLContext context; static bool initialize() { if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { SDL_PrintError(SDL_Init); return false; } gWindow = SDL_CreateWindow(SDL_WINDOW_TITLE, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOW_WIDTH, SDL_WINDOW_HEIGHT, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE); if (gWindow == nullptr) { SDL_PrintError(SDL_CreateWindow); goto err1; } // create OpenGL Context context = SDL_GL_CreateContext(gWindow); if ( !context ) { SDL_PrintError(SDL_GL_CreateContext); goto err2; } return true; err2: SDL_DestroyWindow(gWindow); err1: SDL_Quit(); return false; } static bool initGL() { /* Enable smooth shading */ glShadeModel( GL_SMOOTH ); /* Set the background black */ glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ); /* Depth buffer setup */ glClearDepth( 1.0f ); /* Enables Depth Testing */ glEnable( GL_DEPTH_TEST ); /* The Type Of Depth Test To Do */ glDepthFunc( GL_LEQUAL ); /* Really Nice Perspective Calculations */ glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ); return( TRUE ); } static bool resizeWindow( int width, int height ) { /* Height / width ration */ GLfloat ratio; /* Protect against a divide by zero */ if ( height == 0 ) height = 1; ratio = ( GLfloat )width / ( GLfloat )height; /* Setup our viewport. */ glViewport( 0, 0, ( GLsizei )width, ( GLsizei )height ); /* change to the projection matrix and set our viewing volume. */ glMatrixMode( GL_PROJECTION ); glLoadIdentity( ); /* Set our perspective */ gluPerspective( 45.0f, ratio, 0.1f, 100.0f ); /* Make sure we're chaning the model view and not the projection */ glMatrixMode( GL_MODELVIEW ); /* Reset The View */ glLoadIdentity( ); return( true ); } static void finalize() { SDL_GL_DeleteContext(context); SDL_DestroyWindow(gWindow); SDL_Quit(); } static void render() { /* Clear The Screen And The Depth Buffer */ glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); /* Move Left 1.5 Units And Into The Screen 6.0 */ glLoadIdentity(); glTranslatef( -1.5f, 0.0f, -6.0f ); glBegin( GL_TRIANGLES ); /* Drawing Using Triangles */ glColor3f( 1.0f, 0.0f, 0.0f ); /* Red */ glVertex3f( 0.0f, 1.0f, 0.0f ); /* Top Of Triangle */ glColor3f( 0.0f, 1.0f, 0.0f ); /* Green */ glVertex3f( -1.0f, -1.0f, 0.0f ); /* Left Of Triangle */ glColor3f( 0.0f, 0.0f, 1.0f ); /* Blue */ glVertex3f( 1.0f, -1.0f, 0.0f ); /* Right Of Triangle */ glEnd( ); /* Finished Drawing The Triangle */ /* Move Right 3 Units */ glTranslatef( 3.0f, 0.0f, 0.0f ); /* Set The Color To Blue One Time Only */ glColor3f( 0.5f, 0.5f, 1.0f); glBegin( GL_QUADS ); /* Draw A Quad */ glVertex3f( 1.0f, 1.0f, 0.0f ); /* Top Right Of The Quad */ glVertex3f( -1.0f, 1.0f, 0.0f ); /* Top Left Of The Quad */ glVertex3f( -1.0f, -1.0f, 0.0f ); /* Bottom Left Of The Quad */ glVertex3f( 1.0f, -1.0f, 0.0f ); /* Bottom Right Of The Quad */ glEnd( ); /* Done Drawing The Quad */ /* Draw it to the screen */ SDL_GL_SwapWindow(gWindow); /** NOTE: Sleep 10 msec. */ SDL_Delay(10); } static bool input() { SDL_Event event; /** NOTE: SDL_PollEvent does not sleep while SDL_WaitEvent sleep till event comes. SDL_WaitEvent is more relaxible than SDL_PollEvent. If input is combined with rendering, SDL_WaitEvent cannot be used. */ while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: return true; break; case SDL_WINDOWEVENT: switch( event.window.event ) { case SDL_WINDOWEVENT_RESIZED: /* handle resize event */ SDL_SetWindowSize( gWindow, event.window.data1, event.window.data2 ); resizeWindow( event.window.data1, event.window.data2 ); break; } break; default: break; } } return false; } int main(int argc, char *argv[]) { if (!initialize()) return 1; initGL(); resizeWindow( SDL_WINDOW_WIDTH, SDL_WINDOW_HEIGHT ); while (1) { if (input()) break; render(); } finalize(); return 0; }
Makefile
TARGETS = sdl2_sample all: $(TARGETS) SDL_PREFIX = /mingw64 SDL_CONFIG = $(SDL_PREFIX)/bin/sdl2-config CG_LIBS = CROSS_COMPILE = /mingw64/bin CC = $(CROSS_COMPILE)/gcc CXX = $(CROSS_COMPILE)/g++ CFLAGS = -g -Wall `/bin/sh $(SDL_CONFIG) --cflags` CXXFLAGS = -g -Wall `/bin/sh $(SDL_CONFIG) --cflags` LDFLAGS = `/bin/sh $(SDL_CONFIG) --libs` -Wl,-rpath,$(SDL_PREFIX)/lib LIBS = -lopengl32 -lglu32 -lm clean: rm -f *.o *.a *~ $(TARGETS) sdl2_sample: sdl2_sample.o $(CXX) -o $@ $^ $(LDFLAGS) $(LIBS)
ビルド
$ make
実行
$ ./sdl2_sample.exe
このようなウィンドウが表示されれば成功です
MSYS2 と MinGW のインストール
MSYS2 と MinGW のインストール手順メモ。今回は共に x86_64 版をインストールする
検証環境
Windows10 Home Edition
MSYS2 のインストール
下記サイトから MSYS2 のインストーラをダウンロード、実行してインストールする
(2018-01-17現在、最新版は msys2-x86_64-20170918.exe)
MSYS2 download | SourceForge.net
インストール済みパッケージの更新
MSYS2 のシェルを起動して下記コマンドを実行する
# パッケージデータベースの更新 $ pacman -Sy
# プリインストールされているパッケージを最新版に更新 $ pacman -Suu
以下の警告が出た場合、MSYS2 のシェルをクローズボタンで終了し、再起動して再度 pacman -Suu を実行する
警告: terminate MSYS2 without returning to shell and check for updates again 警告: for example close your terminal window instead of calling exit
以上を更新パッケージが無くなるまで繰り返す。
.bash_profile, .bashrc の作成
環境変数 HOME のディレクトリに次の 2 ファイルを作成する
.bash_profile
test -f ~/.profile && . ~/.profile test -f ~/.bashrc && . ~/.bashrc
.bashrc
# Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi # User specific aliases and functions alias ls='ls -F --color=auto' alias ll='ls -la --color=auto' alias la='ls -a --color=auto' alias sl='ls -F --color=auto'
MinGW-w64 のインストール
MSYS2 のシェルで下記コマンドを実行する
$ pacman -S base-devel $ pacman -S mingw-w64-x86_64-toolchain
動作確認
MSYS2 MinGW 64bit のシェルを起動して下記コマンドを実行
インストールが成功していれば gcc のバージョンが表示される
$ gcc --version gcc.exe (Rev2, Built by MSYS2 project) 7.2.0 Copyright (C) 2017 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.