LoginSignup
20
18

More than 5 years have passed since last update.

Go言語のドキュメントに従って簡単なライブラリとテストコードを作ってみた

Posted at

引き続きHow to Write Go Codeを見ながらGo言語の作業ディレクトリの作り方について調べて見る。

前回:Go言語の作業ディレクトリをドキュメントに従って作り直してみた

今回はGitへのコミット、ライブラリの作成、単体テスト、など。

Gitへのコミット

普通にgit commitするだけです。

$ cd $GOPATH/src/github.com/chooyan/hello
$ git add hello.go 
$ git commit -m "commit hello world program"
[master (root-commit) 6b51615] commit hello world program
 1 file changed, 7 insertions(+)
 create mode 100644 hello.go

これをGithubにpushすれば、世界中の人がgo getできるようになるってことかな。

ライブラリの作成

mathパッケージを作って、引数を足して返すだけのSum関数を実装したsum.goをhello.goにインポートして使ってみる。

$ mkdir math ; cd math
$ vi sum.go
sum.go
package math

func Sum(a int, b int) int {
        return a + b
}

パッケージ名、ディレクトリ名等は以下に従うと良いみたい。

where name is the package's default name for imports. (All files in a package must use the same name.)
Go's convention is that the package name is the last element of the import path: the package imported as "crypto/rot13" should be named rot13.

つまり、.goファイルを置いてあるディレクトリをそのままパッケージ名として宣言する。同じディレクトリに置いてある.goファイルのパッケージ名は全て同じになる。

以上を踏まえて、ライブラリを作ったらgo buildしておく

$ go build github.com/chooyan/math

ライブラリの場合、ビルドしたファイルは/pkgの下に生成される。

$ ll /usr/local/workdir/go/pkg/darwin_amd64/github.com/chooyan/
total 8
-rw-r--r--  1 chooyan staff  1048  8 26 23:53 math.a

/pkg下のディレクトリは、ビルドを実行したマシンによって分けられる様子。

The linux_amd64 part is there to aid in cross-compilation, and will reflect the operating system and architecture of your system.

ライブラリを使ってみる

では、作ったmathパッケージをhello.goで使うように変更する。

$ cd $GOPATH/src/github.com/chooyan/hello
$ vi hello.go
hello.go
package main

import (
    "fmt"

    "github.com/chooyan/math"
)

func main() {
    x := 10
    y := 4
    fmt.Printf("Hello, world.\n")
    fmt.Printf("Sum of %d and %d is %d", x, y, math.Sum(x, y))
}

修正したら、go installして実行する。

$ go install github.com/chooyan/hello
$ hello
Hello, world.
Sum of 10 and 4 is 14

できた。

ライブラリをテストする

最後に、testingパッケージを使った簡単なテストコードを書いてみる。

You write a test by creating a file with a name ending in _test.go that contains functions named TestXXX with signature func (t *testing.T).

なので、今回はmathディレクトリ配下にsum_test.goファイルを作り、そこにTestSum関数を定義する。

$ cd $GOPATH/src/github.com/chooyan/math
$ vi sum_test.go
sum_test.go
package math

import "testing"

func TestSum(t *testing.T) {
    a := 10
    b := 20
    expected := 30
    actual := Sum(a, b)
    if expected != actual {
        t.Errorf("Sum(%d, %d) == %d, want %d", a, b, actual, expected)
    }
}

実行する。

$ go test github.com/chooyan/math
ok      github.com/chooyan/math 0.037s

ちなみに、Sum関数をいじってわざと失敗するとこんな感じ。(結果の出力だけ記載)

$ go test github.com/chooyan/math
--- FAIL: TestSum (0.00s)
    sum_test.go:11: Sum(10, 20) == 29, want 30
FAIL
FAIL    github.com/chooyan/math 0.010s

以上。
写経って大事。

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