Learning Golang, but don't know where to start? Golang (Go) is a programming language created at Google, known for its simplicity, efficiency and strong support for concurrent programming. It could be an excellent choice if you are a beginner looking to step into the world of coding.
I have specially designed this Golang tutorial for knowledge seekers like you. It will walk you through writing your very first Go program: the classic 'Hello, World!'. It is the most basic thing you should start your coding practice with.
Go is an open-source programming language created by Google in 2009. It is designed to be simple, fast and efficient to make it ideal for beginners and professionals. Go is widely used for building web servers, cloud applications and microservices.
The Hello World program is a tradition in programming. It will introduce you to the basic structure of a Go program that includes packages, imports and the main function. It also helps you verify that your Go environment is set up correctly.
You need to set up your development environment before writing your first Go program. It involves the following steps:
Related Article: How to install Go programming language?
Let's create a simple Go program that prints 'Hello, World!' on your screen. The steps are:
1. Create a File: Open your code editor and create a new file named hello.go. Go files always end with the .go extension.
2. Add the Package Declaration: Every Go program starts with a package. Use package main for executable programs. This tells the Go compiler that this is a standalone program.
| package main |
3. Import the fmt Package: The fmt package in Go handles formatted input and output. Import it using:
| import "fmt" |
4. Write the Main Function: The main function is the entry point of your Go program. Inside it, use fmt.Println to display text.
func main() { fmt.Println("Hello, World!") } |
5. Save the File: Save your hello.go file in a folder like go-projects.
Your complete Go Hello World program looks like this:
package main import "fmt" func main() { fmt.Println("Hello, World!") } |
Running your Go Hello World program involves opening your terminal and navigating to the folder containing hello.go. Use this command for the same:
| go run hello.go |
Output:
| Hello, World! |
You can also use the given command if you want to compile the program into an executable file:
| go build hello.go |
This will create an executable file (hello on macOS/Linux or hello.exe on Windows) that you can run directly.
Also Read: Golang vs Java
This guide has explained how to set up your Go environment, write a simple program and run it using the Go compiler. Its simplicity makes it perfect for beginners and this basic program lays the foundation for learning more advanced Go concepts. Just keep practicing, keep coding, explore Go tutorials and try building small projects to grow your skills.
The package main declaration tells the Go compiler that the program is executable. It must include a main function that is the entry point of the program.
The fmt package provides functions for formatting and printing text like fmt.Println. It is used to display output in the terminal.
Save your program with a .go extension like hello.go, open your terminal, navigate to the file's directory and run go run hello.go.
Yes, Go has simple syntax and is easier to understand compared to many other programming languages.
Course Schedule
| Course Name | Batch Type | Details |
| Golang Training | Every Weekday | View Details |
| Golang Training | Every Weekend | View Details |