Introduction to Go (Golang)
Theory
A major tool of computer science is programming languages. We use them to develop computer programs. In the modern world, there is a wide variety of programming languages, every one with its own community of fans. Let's find out what makes Go unique and what's there to like about this language.
§1. Go programming language?
Just a very brief history note if you're curious:
In 2007, Google began developing the Go programming language as an internal project. Two Unix luminaries, Rob Pike and Ken Thompson, designed the original. Two years later, on November 10, 2009, the software was released under a liberal open-source license. A version of Go 1.0 that was ready for production was released in March 2012. Google's team of designers worked on the Go project along with Russ Cox, Andrew Gerrand, Ian Lance Taylor, and others. And thanks to all these people, you've got the chance to learn this programming language.
Go is also called Golang, the same name as the Go programming language; the "Golang" moniker arose because the website (go.dev) was originally golang.org. We will use both names interchangeably throughout this topic.
§2. Advantages of Go
Go has a clean syntax. The table below displays a syntax comparison between Go and Java when declaring and initializing variables.
Go
package main
func main() {
a, b, c := 1, 2, 3 // a, b, c have the 'int' type
}
Java
public class Main {
public static void main(String[] args) {
int a = 1, b = 2, c = 3;
}
}
Go is a pragmatic language: its main purpose is to solve real-world problems. You can see it in the below examples:
- The original intent of Go was to be used in the server-centric world of web servers and storage architecture. Thus came the decision to make Go a programming language compiled into native code. Go programs usually execute very fast and have a low memory footprint. That's why you can use this language as a system programming language.
- Golang excels at handling massive concurrency and making event processing easy. That makes it a good choice for game servers and IoT solutions. Another helpful link to learn about successful examples in Go is the Github wiki.
- And finally, you can use Go as a general programming language. It is a great tool for developing and solving text-processing problems.
Take a pause to look at how Go mascots work in different roles.
The language supports a range of programming paradigms, including imperative programming, object-oriented programming, procedural, and functional programming. The Go FAQ contains an explanation of these designs by the Go team.
§3. Disadvantages of Go
While Go has many advantages over other programming languages, it still has some disadvantages you should be aware of when learning it:
- Go's standard library lacks some functions compared to other languages. For example, there is no built-in function to find the biggest number in an array of numbers, so you would have to implement it manually.
- Go's standard library doesn't support GUI programming by default, which is an advantage of other programming language libraries, such as the Java standard library.
- Go's standalone binaries are large because they require the embedded garbage collector and the entire Go runtime as well. The "Hello World" program might consume 2MB in file size.
Gophers are just simple beings.
§4. First Go example
Now that you've been acquainted with the Go language, you can follow these steps to install Go on your computer.
Once you have finished installing Go, we can take a look at the first code example. It prints the "Hello Golang" string to the terminal (suppose this example is contained within the main.go file):
Go Example
package main
import "fmt"
func main() {
fmt.Println("Hello Golang") // prints "Hello Golang" to the screen
}
When we write comments in Go, we use the C++ style: // for one-line comments that we usually place at the end of the code line, and /* ... */ for multi-line comments.
The Go programming language is organized into packages, and every Go program should have the main package containing the main() function, which serves as the entry point for the program (the function that is executed first).
Now click the run button in JetBrains' GoLand (or IntelliJ IDEA) or run the program directly in the terminal by executing the go run main.go command.
Congrats! You've just used Go for the first time.
§5. Summary
Here is a reminder of what we have covered so far about the Go language:
- A brief description of Go;
- What it can do;
- Reasons why Golang is so popular in computer programming;
- Learning how to write simple code in Golang.
Once you understand Go's most basic concepts, you're ready to go further with your exploration.
You can also view Lesson on hyperskill.org.
Practical tasks and answers
Tasks and answer options are given. The correct option is highlighted in blue color.
№1. Characteristics of the Go language
Question: Select the correct statements about characteristics of the Go language.
Select one or more options from the list:
- It can be used as a general programming language ✔
- It supports multi-paradigms programming ✔
- It has a complex and hard to read syntax
- It supports concurrency programming ✔
Explanation. The statement that it has a complex and hard to read syntax is incorrect. Go has a relatively simple syntax that is similar to C and Java.
Here are some additional details about the characteristics of the Go language:
- General-purpose language: Go can be used to develop a wide variety of applications, including web applications, command-line tools, and distributed systems.
- Multi-paradigm: Go supports multiple programming paradigms, including procedural, object-oriented, and functional programming.
- Concurrency: Go has built-in support for concurrency, which allows multiple tasks to run simultaneously. This makes it well-suited for developing applications that need to handle a lot of requests or data.
Overall, Go is a powerful and versatile language that can be used to develop a wide variety of applications. It is a good choice for developers who want a language that is simple, efficient, and scalable.
№2. Package and entry point function name
Question: What is the name of the package and the name of the function that every executable Go program should have?
Select one option from the list:
- package main and func main() ✔
- package main and func init()
- package init and func main()
№3. Main applications of the Go programming language
Question: Go is a general-purpose language that can be used for many purposes. Which of the following options represent some of the most common uses for Go?
Select one or more options from the list:
- Internet of things platform ✔
- Command-line application ✔
- Acting as a markup language like markdown or HTML
- Web application ✔
Explanation. The most common uses for Go are:
- Command-line applications: Go is a great language for writing efficient and scalable command-line applications. It is often used for building tools for system administration, DevOps, and cloud computing.
- Web applications: Go is a popular choice for building web applications because it is fast, scalable, and easy to learn. It is also well-suited for microservices architecture.
- Internet of Things (IoT) platforms: Go is a good choice for building IoT platforms because it is efficient and can handle a large number of connections. It is also well-suited for real-time applications.
- Data science and machine learning: Go is a relatively new language in the data science and machine learning space, but it is gaining popularity due to its speed and scalability. It is also well-suited for distributed computing.
So the answer is Command-line application, Web application, and Internet of things platform.
The option Acting as a markup language like markdown or HTML is not a common use for Go. Markdown and HTML are markup languages that are used to format text, while Go is a programming language that is used to create applications.
№4.Run the program in the terminal
Question: Suppose you have a Go program in your current working directory contained within the main.go file. What command must be executed in the terminal to run the Go program?
Select one option from the list:
- go exec main.go
- go run main.go ✔
- go build main.go
- run main.go
Explanation. The correct command to run the Go program is go run main.go.
The go run command compilers and runs the Go program in one step. The go build command compiles the Go program, but does not run it. The go exec command executes a binary file, which is a compiled Go program. The run main.go command is not a valid command.
№5. Disadvantages of the Go programming language
Question: Choose the correct statements about the disadvantages of the Go language.
Select one or more options from the list:
- Go's standard library lacks some functions compared to other programming languages ✔
- Go doesn't support object-oriented programming
- Go's standard library doesn't support GUI programming ✔
- Go programs execute slow and have a high memory footprint
Explanation. The correct statements about the disadvantages of the Go language are: Go's standard library lacks some functions compared to other programming languages; Go's standard library doesn't support GUI programming.
Go does support object-oriented programming, and Go programs are known for their fast execution and low-memory footprint.
№6. A great birthday
Question: When did Google begin developing the Go programming language as an internal project?
Select one option from the list:
- 1993
- 1985
- 2007 ✔
- 2019
Explanation. Google began developing the Go programming language as an internal project in 2007.
The Go language was designed by Robert Griesemer, Rob Pike, and Ken Thompson. It was designed to be a simple, reliable, and efficient language that would be well-suited for building large-scale distributed systems.
Go was released as an open-source project in 2012. It has since become a popular language for a variety of tasks, including web development, system programming, and cloud computing. So the answer is 2007.
The other options are incorrect.
- 1993: This is the year that Google was founded. The Go language was not developed until 2007.
- 1985: This is the year that the C programming language was released. Go was designed to be a successor to C, so it is unlikely that Google would have begun developing Go in 1985.
- 2019: This is the year that the Go 1.13 release was announced. The Go language was already in development by 2019.
№7. Running your first Go program
Question: Below you will see the code of a very simple Go program that outputs a message and the result of an arithmetic operation:
package main
import "fmt"
func main() {
var greeting = "Hello, Go!"
fmt.Println(greeting)
var num1, num2 = 1, 2
fmt.Println(num1 / num2)
}
Your task is to copy the above code, visit the Go playground and paste the code into the online editor.
After pasting the code, click the Run button near the top-right corner, and then select the correct output of the program below.
Select one option from the list:
- Hello, Go!
0 ✔ - Hello, Go!
0.5 - Hello, World!
0 - Hello, Go!
1
№8. Is Go properly installed?
Question: Suppose you have just finished installing Go in your computer after following the official Go installation guide. What command does the Go installation guide suggests to execute in your terminal to verify that you've installed Go?
Select one option from the list:
- go install
- go version ✔
- go run
- go build
Explanation. The command to verify that Go has been installed correctly is go version. The go version command prints the version of Go that is installed on your computer. If Go is installed correctly, you should see the version number printed to the console.
The other options are not correct.
- go install installs a Go package.
- go run compiles and runs a Go program.
- go build compiles a Go program, but does not run it.
What is the Go programming language? Short introduction to Go for beginners.
Go is a statically typed, compiled programming language designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson. It is syntactically similar to C, but also has memory safety, garbage collection, structural typing, and CSP-style concurrency.
Go is a general-purpose language that can be used for a variety of tasks, including:
- Web development
- System programming
- Cloud computing
- Data science and machine learning
- IoT platforms
Go is known for its simplicity, reliability, and efficiency. It is also a very fast language, and it can be used to build scalable and high-performance applications.
Here are some of the key features of Go:
- Static typing: Go is a statically typed language, which means that the types of variables and expressions must be known at compile time. This helps to prevent errors and makes code more reliable.
- Garbage collection: Go has garbage collection, which means that the programmer does not need to worry about memory management. This makes Go code easier to write and maintain.
- Structural typing: Go uses structural typing, which means that the type of variable is determined by its structure, not its declaration. This makes code more flexible and easier to read.
- CSP-style concurrency: Go supports CSP-style concurrency, which allows multiple tasks to run concurrently. This makes Go well-suited for building scalable and high-performance applications.
If you are a beginner to programming, Go is a good language to learn. It is a simple language with a clean syntax, and it is also a powerful language that can be used to build a variety of applications.
Here are some resources to help you get started with Go:
- The Go Programming Language: https://go.dev/ - The official Go website, which includes documentation, tutorials, and tools.
- Go by Example: https://gobyexample.com/ - A collection of short code examples that illustrate the Go programming language.
- The Go Blog: https://blog.golang.org/ - The official Go blog, which includes articles about Go news, features, and best practices.