Go の基礎

Hello World in Go: Structure of a Program

Write your first Go program and see what each part does: package main, import and func main.

  • 初級
  • 約2分で読めます
  • 実行できるサンプル 1 件
学べること
  • package main and func main
  • Importing fmt
  • Printing with fmt.Println
From source to output
  1. 1main.goYou write the code
  2. 2go runGo compiles and starts it
  3. 3main()The program starts in main
  4. 4Hello, Go!The text appears

How a Go program runs

Every Go file starts with a package name. A program that you can run lives in package main and starts in the function main. import brings in other packages, such as fmt for printing.

fmt.Println prints its arguments and ends the line. Names that start with a capital letter, such as Println, are the ones a package makes available to other packages.

重要なポイント

  1. A runnable program lives in package main.
  2. It starts in func main.
  3. fmt.Println prints a line of text.

実行できる Go のサンプル

Go
コードを実行 →
main.go
package main

import "fmt"

func main() {
	fmt.Println("Hello, Go!")
}
この例の実行ボタンを作成 実行結果
Hello, Go!

練習

サンプルをオンラインコンパイラーで開き、値を変えたり行を追加したりして、実行する前に出力を予想してみましょう。次に、わざとコードを壊してコンパイラーのメッセージを読んでみてください。

このレッスンを終えましたか?

学習済みにすると進み具合を記録できます。

次へ: Variables and Constants →

チャレンジで練習する

このレッスンの内容を使って実際の問題を解きましょう。各チャレンジはブラウザで動き、すぐにテストされ、AI のレビューも受けられます。

すべてのチャレンジ