Getting Started with Go Programming
In this tutorial, you will learn how to get started with Go Programming and write your first Go program.
The Go is an open-source programming language developed by Google. It was created with below 3 things in mind.
- Efficient Compilation
- Efficient Execution
- Ease of Programming
That’s why GoLang is one of the fastest growing and adapting programming language. It’s fast, expressive, easy to read and write code, and lightweight.
Running Go Program
The online Go compiler makes life very easy for GoLang learner, simply use The Go Playground.
To run Go Program locally on your computer, you can easily install and use it using Install Go on Windows, Linux, and macOS.
Writing your First Go Program
Let’s start with the basic syntax of Go Programming.
// Print "Hello World!" message
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
}
Note: The line starts with
//is a comment. The comments are used to help users to understand the code, they are completely ignored by the Go compiler.
Let’s understand the different parts of the program.
The main() function
All Go programs start with the main() function.
We must import the main package using the package main statement to use this function.
Printing “Hello World!”
We use Println() function to print a String in Go. This Println() function can be used by importing the fmt package using import "fmt" statement.
Take away
The following are the take away from this tutorial.
- All Go programs execution start from the
main()function. - To use any function from a package, we need to import that package first using the
importkeyword. - The basic structure of a Go program is as below.
package main
func main() {
}
Now you are good to start with Go Programs. You wrote your first Go Program and executed it with ease.
Frequently Asked Questions
Q: Do I need to install Go to start learning?
No. You can use The Go Playground to write and run Go programs directly in your browser. However, for larger projects, installing Go locally is recommended.
Q: What is the Go Playground?
The Go Playground is a free online tool provided by the Go team that lets you write, compile, and run Go code in your web browser without installing anything on your computer.
Q: What does package main mean?
The package main statement tells the Go compiler that this package should compile as an executable program rather than a shared library. Every standalone Go program must belong to the main package.
Q: Can I write Go code in any text editor?
Yes. Go code is plain text, so you can use any text editor. However, editors like VS Code with the Go extension provide features like syntax highlighting, auto-completion, and debugging support that make development easier.