golang入門してみた

はてなブログ卒業して、githubでブログ書くつもりでツール選定した結果
spf13/hugo · GitHub
hugoっていうgo製のものに決めたのでgolangに入門してみた

まずはwindowsmsiダウンロード&インストール

Downloads - The Go Programming Language
A Tour of Goを試すのが王道みたいだけど、

5分動画で、実行モジュール、パッケージ、テストをさくっと試す

Writing, building, installing, and testing Go code - YouTube

windowsのパスにPATHとGOPATHを追加して

C:\Users\USERNAME\gocode     // GOPATH
C:\Users\USERNAME\gocode\bin // PATH
実行モジュール

C:\Users\USERNAME\gocode\src\github.com\pdevty\hello\hello.go

package main

import "fmt"

func main() {
	fmt.Println("hello go")
}

コンパイル&実行

cd C:\Users\USERNAME\gocode\src\github.com\pdevty\hello
go install
hello // hello go
パッケージ

C:\Users\USERNAME\gocode\src\github.com\pdevty\string\string.go

package string

func Reverse(s string) string {
	b := []byte(s)
	for i := 0; i < len(b)/2; i++ {
		j := len(b)-i-1
		b[i], b[j] = b[j], b[i]
	}
	return string(b)
}

コンパイル

cd C:\Users\USERNAME\gocode\src\github.com\pdevty\string
go build
go install

C:\Users\USERNAME\gocode\src\github.com\pdevty\hello\hello.go

package main

import (
	"fmt"
	"github.com/pdevty/string"
)

func main() {
	fmt.Println(string.Reverse("hello go"))
}

コンパイル&実行

cd C:\Users\USERNAME\gocode\src\github.com\pdevty\hello
go install
hello // og olleh
テスト

C:\Users\USERNAME\gocode\src\github.com\pdevty\string\string_test.go

package string

import "testing"

func Test(t *testing.T) {
	var tests = []struct {
		s, want string
	}{
		{"hello go", "og olleh"},
		{"", ""},
	}
	for _, c := range tests {
		got := Reverse(c.s)
		if got != c.want {
			t.Errorf("Reverse(%q) == %q, want %q", c.s, got, c.want)
		}
	}
	
}

テスト

cd C:\Users\USERNAME\gocode\src\github.com\pdevty\string
go test // PASS ...

いけたー
これでhugoためせる

参考
Writing, building, installing, and testing Go code - YouTube
Get Started with Go - YouTube
Goプログラミング言語のチュートリアル - golang.jp
Git初心者でも大丈夫!完全無料でGithub PagesにWebページを公開する方法 | 株式会社LIG
所要時間3分!? Github PagesとHEXOで爆速ブログ構築してみよう! | 株式会社LIG
A Tour of Go
Documentation - The Go Programming Language
Go言語で作る webアプリ@gocon 2013 spring
Martini(+Ginkgo)をWerckerでCIしてHerokuにデプロイ | SOTA