Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Go Dictionary
  3. Creating and Running .go Files

Creating and Running .go Files

This page explains how to save Go code as a text file and run it in the terminal. The file itself is simply a plain text file saved with a .go extension.

How to write a .go file

Write your Go code in a text editor and save the file with a .go extension. Save the file using the UTF-8 character encoding.

sample_greet.go
package main

import "fmt"

func main() {
	name := "Kiryu Kazuma"
	age := 27

	fmt.Printf("Hello, %s!\n", name)
	fmt.Printf("Age: %d\n", age)

	if age >= 20 {
		fmt.Println("Adult.")
	} else {
		fmt.Println("Minor.")
	}
}

The first line, package main, indicates that this file is an executable program. The second line, import "fmt", imports the package that provides standard output and other functionality. Execution begins from the main function, which is the entry point of the program.

How to write comments

You can write comments (notes) in a .go file. Comments are ignored by the compiler, so they are useful for leaving descriptions or notes about the code.

SyntaxDescription
// commentA single-line comment. Write it with one space after //. Everything from // to the end of the line is a comment.
/* comment */A multi-line comment. Everything from /* to */ is a comment.
sample_comments.go
package main

import "fmt"

/*
  The following is the main function.
  This is the entry point of the program.
*/
func main() {
	name := "Majima Goro"

	// Displays a greeting message.
	fmt.Printf("Hello, %s!\n", name)
}

This produces the following output:

go run sample_comments.go
Hello, Majima Goro!

In Go, a // comment written immediately before a function, type, or variable is treated as a documentation comment. It is a good habit to write comments for exported symbols.

How to run

Run directly with go run

The go run command compiles and runs in a single step. It is convenient when you want to try out code quickly.

go run greet.go
Hello, Kiryu Kazuma!
Age: 27
Adult.

Build with go build and then run

The go build command generates an executable file. The generated executable can run even in environments where Go is not installed.

go build greet.go

When the build succeeds, an executable file is generated in the current directory. If no error message is displayed, the build was successful. Run it with ./.

./greet
Hello, Kiryu Kazuma!
Age: 27
Adult.

To check the version, use the following command.

go version

Summary

A .go file is simply a plain text file. You can create one by writing Go code in a text editor and saving it with a .go extension. No special tools are needed.

There are two ways to run Go code. go run filename.go compiles and runs at once, which is convenient when you want to try code quickly. On the other hand, go build filename.go generates an executable, making it suitable for distribution or use in production environments.

There are two types of comments: single-line comments (//) and multi-line comments (/* */). In Go, single-line comments are used almost exclusively.

For information on installing Go, see Setup.

If you find any errors or copyright issues, please .