0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Go言語でモジュールを作成し、GoDocを使ってドキュメントを作成する

Posted at

はじめに

Go言語についてと開発環境の準備 でGo言語についてと開発環境の方法を学びました。

Go言語でモジュールを作成し、GoDocを使ってドキュメントを作成する方法について備忘録としてまとめました。

GoDocとは

GoDocはルールに沿ってソースコードにコメントを記述することで、パブリックな関数、type、変数についての説明や実装例をドキュメントとしてホストしているウェブサイトにまとめてくれます。また、コマンドライン上でもドキュメントを閲覧することが出来ます。

参考にしたGo言語学習本

参考サイト

https://godoc.org/golang.org/x/tools/cmd/godoc
https://blog.golang.org/examples

事前準備

Go言語についてと開発環境の準備 を読んでGo言語の開発環境を準備します。

go getでGoDocのパッケージをインストールします。

$ go get golang.org/x/tools/cmd/godoc

テスト実装

Package stringutil をサンプルに実際にドキュメントを作成してみました。

サンプルコード
https://github.com/golang/example
https://pkg.go.dev/github.com/golang/example/stringutil/

package stringutil_test

import (
    "fmt"

    "github.com/golang/example/stringutil"
)

func ExampleReverse() {
    fmt.Println(stringutil.Reverse("hello"))
    // Output: olleh
}

関数コメントや ExampleReverse で使用例を example_test.go に記述し、テストを実行します。

$ go test -v
=== RUN TestReverse
--- PASS: TestReverse (0.00s)
=== RUN: ExampleReverse
--- PASS: ExampleReverse (0.00s)
PASS
ok  	github.com/golang/example/stringutil	0.009s

ドキュメントをWebページとして表示します。

$ godoc -http =:6060

reverse.png

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?