0
0
Goprogramming~5 mins

Writing first Go program

Choose your learning style9 modes available
Introduction

Writing your first Go program helps you learn how to create and run simple code. It shows you the basic structure of a Go program.

When you want to learn how to write and run Go code for the first time.
When you want to test if your Go setup on your computer works correctly.
When you want to understand the basic parts of a Go program like package, import, and main function.
Syntax
Go
package main

import "fmt"

func main() {
    fmt.Println("Hello, world!")
}

package main tells Go this is an executable program.

func main() is where the program starts running.

Examples
This prints a simple greeting message.
Go
package main

import "fmt"

func main() {
    fmt.Println("Hello, Go!")
}
You can change the message inside the quotes to print anything you want.
Go
package main

import "fmt"

func main() {
    fmt.Println("Welcome to Go programming")
}
Sample Program

This program prints the message "Hello, world!" to the screen. It shows the basic structure of a Go program.

Go
package main

import "fmt"

func main() {
    fmt.Println("Hello, world!")
}
OutputSuccess
Important Notes

Always save your Go files with the .go extension.

Use the command go run filename.go to run your program in the terminal.

Make sure Go is installed and your environment is set up before running the program.

Summary

Every Go program starts with package main and a main function.

Use fmt.Println to print messages to the screen.

Running your first program helps you check your setup and understand Go basics.