0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【メモ】A Tour of Go を写経する

Last updated at Posted at 2025-12-10

実務でGoを扱うので、改めて A Tour of Go を写経しながら、自分用メモとして更新していきます。

Hello, World

package main

import "fmt"

func main() {
	fmt.Println("Hello, 世界")
}

実行結果:

Hello, 世界

メモ:

  • Go のプログラムは package mainfunc main() が必要
  • gofmtがgo標準でインストールされているので、ファイルを保存すると自動でコードを整形してくれる
  • ビルドは go build <ファイル名>
  • 実行は go run <ファイル名>

Packages

package main

import (
	"fmt"
	"math/rand"
)

func main() {
	fmt.Println("My favorite number is", rand.Intn(10))
}

実行結果:

My favorite number is 6

メモ:

  • すべての Go ファイルは先頭に package 文を書く
  • 同じディレクトリの .go ファイルは原則すべて同じ package 名にしなければならない
  • ディレクトリ名とパッケージ名は慣習的に一致させるが、仕様上は一致必須ではない
  • エントリポイントは package main 内の func main()

Imports

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("Now you have %g programs.\n", math.Sqrt(7))
}

実行結果:

Now you have 2.6457513110645907 problems.

メモ:

  • import で複数パッケージをまとめてインポート
  • 1行ずつ書くこともできるが、Go のコードスタイルではまとめて書くことが推奨されている

Exported names

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Pi)
}

実行結果:

3.141592653589793

メモ:

  • Goでは、名前の最初の文字で、公開される範囲が決まる
  • 大文字で始まる名前 = エクスポート(公開)
    • 外部パッケージから参照可能
    • 例: math.Pi, math.Sqrt
  • 小文字で始まる名前 = 非エクスポート(非公開)
    • 同じパッケージ内でのみ参照可能
    • 例: math.pi, math.hoge

Functions

package main

import "fmt"

func add(x int, y int) int {
	return x + y
}

func main() {
	fmt.Println(add(42, 13))
}

実行結果:

55

メモ:

  • Goでは引数で、変数名の 後ろ に型名を書く
    • 例:add(x int)

Functions continued

package main

import "fmt"

func add(x, y int) int {
	return x + y
}

func main() {
	fmt.Println(add(42, 13))
}

実行結果;

55

メモ:

  • 引数の型を省略してまとめて書くことができる (x, y int)
  • 実務でも2~3個の引数が同じ型ならまとめて可読性上げることもある
  • 4つ以上の場合は構造体を定義して、構造体を引数にした方が無難

Multiple results

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?