Welcome to the world of Go! Go, also known as Golang, is a statically typed, compiled programming language designed by Google. It’s known for its simplicity, efficiency, and powerful features for building scalable and concurrent applications.
In this post, we will guide you through writing your first “Hello, World!” program in Go. Whether you’re a beginner or just exploring Go, this guide will help you get started.
Setting Up Your Environment
Before writing any code, ensure you have the following:
- Go Installed: Download and install Go from the official website.
- A Code Editor: Use any text editor, such as Visual Studio Code, GoLand, or Sublime Text.
Writing Your First Go Program
- Open your text editor.
- Create a new file and name it
hello_world.go
. - Add the following code:
package main
import "fmt"
func main() {
// This is your first Go program
fmt.Println("Hello, World!")
}
Explanation of the Code
package main
: Defines the package asmain
, which is required for the entry point of the application.import "fmt"
: Imports thefmt
package, which provides formatted I/O functions.func main()
: Themain
function is the entry point of a Go program.fmt.Println("Hello, World!")
: Prints “Hello, World!” followed by a newline to the console.
Running Your Go Program
- Save the file.
- Open a terminal or command prompt.
- Navigate to the directory where you saved the file.
- Run the program by typing:
go run hello_world.go
You should see the output:
Hello, World!
Alternatively, you can compile the program into an executable by typing:
go build hello_world.go
Then run the executable:
./hello_world
Congratulations!
You’ve successfully written and executed your first Go program. Go’s simplicity and performance make it a favorite among developers for building modern applications.
What’s Next?
Now that you’ve written your first program, explore more Go features like variables, loops, functions, and concurrency. Go opens the door to building robust and efficient software.
Happy coding!