
{{ $('Map tags to IDs').item.json.title }}
How to Write Your First Go Program
Go, also known as Golang, is a statically typed, compiled programming language designed for simplicity and high performance. Writing your first Go program is an exciting step in learning this powerful language. This tutorial will guide you through the steps of creating and running your first Go program.
1. Setting Up Your Environment
Before you start writing Go programs, ensure that you have installed Go on your system. You can check if Go is installed by running:
go version
If it’s not installed, refer to our guide on how to install Go.
2. Creating a Workspace
Create a directory for your Go workspace. This is where all your Go projects will reside. Open your terminal and run:
mkdir -p ~/go/src/hello
cd ~/go/src/hello
This creates a directory called hello
where we’ll write our first program.
3. Writing Your First Go Program
Create a new file named main.go
:
nano main.go
Insert the following code into main.go
:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
This code defines a simple Go program that prints Hello, World!
to the console.
4. Running Your Go Program
To run your program, make sure you are in the same directory as main.go
and execute:
go run main.go
The output should display:
Hello, World!
5. Compiling Your Go Program
If you want to compile your Go program into an executable, use:
go build main.go
This command will create an executable named hello
in the same directory. To run the compiled program, use:
./hello
Running the executable should give the same output:
Hello, World!
6. Conclusion
By following this tutorial, you have successfully written and run your first Go program. The Hello, World!
application is a great starting point to learn how Go works. Continue to explore the features of Go, including its rich standard library, to enhance your programming skills!