Go is an open-source programming language developed by Google. It was created with these things in mind:
- efficient execution
- efficient compilation
- ease of programming
That's why Go is very fast, expressive (easy to read and write code) and lightweight.
Running Go
The simplest way of running Go code is by using an online Go compiler like The Go Playground.
You can also easily install and run Go programming locally on your computer. For that, visit Install Go on Windows, Linux, and macOS.
Writing Our First Go Program
To get familiar with the basic syntax of Go programming, let's write our first program.
// Print "Hello World!" message
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
}
Output
Hello World!
Here are the different parts of this program.
1. The main() function
All Go programs start with the main()
function.
func main() {
// code goes here
}
To use this function, we must import the main
package first using package main
.
Note: The line that starts with //
is a comment. Comments are used to help users understand the code; they are completely ignored by the Go compiler.
2. Print a line of text
To print a line of text in Go, we use the fmt.Println()
function. The function displays the content from the parentheses ()
.
To use fmt.Println()
, we must import the fmt
package first using import "fmt"
.
Basic Structure of a Go Program
Here are the things to take away from this tutorial.
- All Go programs start from the
main()
function. - The bare minimum code structure of a Go program is:
package main
fun main() {
}
Frequently Asked Questions
The official name of the language is plain Go.
The confusion between Go and Golang arose for the following reasons:
- The official Go website is golang.org; not go.org as this domain was not available.
- People started using the word Golang for their ease; assuming that Go(lang) is the short form of Go Language.
Originally, Go was developed to program networking and infrastructure-related stuff.
Today, Go has a wide range of applications like
- Data science and artificial intelligence.
- Network Server programming, Web programming.
- Robotics, Internet of Things, games, and microcontroller programming.
- Cloud-based applications and server-side applications.
- Data Scraping.
- In the future, Golang applications can run in androids as well.
Go programming is for you if you are looking for these features:
- built-in testing
- support concurrency (can run multiple tasks at a time)
- fast, simple and lightweight
- efficient execution
- efficient compilation
- ease of programming
- interfaces to write modular and testable code
- garbage collection
Go has features of a structural and object-oriented language. So, the answer can be both yes and no.
Go allows an object-oriented style of programming but it has no type of hierarchy.