Introduction
Go Programming language also known as Golang is an open-source programming language that was developed by Google to create simple, reliable, and efficient software. There are some reasons why Golang is worth to learn :
- Developed by the experts
- The syntax easy to understand
- Used in web development, especially backend development
Installing Go in Local Machine / Your Development Environment
The installation guide for Go is available here
After the installation is finished, you can write a Go program in your favorite text editor such as VSCode, or use a dedicated IDE like Goland.
Golang playground is also available for learning the Go programming language: https://go.dev/play/
Writing First Program
To check that Go is installed correctly on your local machine, type this command.
go version
To check the Go environment setup, use this command.
go env
Alright, let's create the file called main.go
to write the first program. Create that file inside the Go workspace directory based on the Go environment setup.
To write a Go code outside the Go workspace directory. Use the Go module instead.
Let's write the first program in the main.go :
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello, playground")
}
To execute the program, run this command in your working directory that is used to write that program.
go run main.go
Here it is the output :
Hello, playground
The flow of the program that has already been created is like this:
- Declare the package main
- Import the required library (for example the syntax import "fmt" package)
- declare the main function of the program, the func main() executes all the syntax inside it.
- From the fmt package, use the function called Println() to write the output to the console
package main //1
import (
"fmt" //2
)
func main() { //3
fmt.Println("Hello, playground") //4
}
Briefly, The flow of execution uses a top-down approach.
FYI, the Uppercase letter in the method name (Println) means that the function is in a package scope.
Write a Go Program with Go Module
The go module can be used to store Go codes and their dependencies (if dependencies are required) outside the Go workspace. This is an example of writing a Go program with a Go Module.
Create a new folder with any name, for example,
first-go-program
.Open that directory with a text editor or IDE.
Initialize the Go module.
go mod init first-go-program
- Create a new file called
main.go
then write this code.
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello, playground")
}
- Run the program.
go run main.go
This is the output of the program
Hello, playground
Variables
The variable is a mechanism to store a value in a certain place. With a variable, we can store any value with a specific type like string, int, or even array.
Go has many types of value/data that can be stored inside a variable. The list of Go data/value types are :
- Boolean:
bool
(true/false) - Numeric types :
int
,float64
,uint8
(other numeric types) - String:
string
- Array: e.g.
[4]int{1,2,3,4}
- Slice: e.g.
[]int{1,2,3,4}
The other types can be checked here.
In numeric types, there are two main types: unsigned (uint8, uint 32) and signed (int, float64). The meaning of unsigned is only the positive number that is available in the type. The signed like int and float64 are available for positive and negative numbers.
Here is an example of assigning a value inside a variable in Go.
var name string = "Using var keyword" //1 using var keyword
data := 42 //2 using shorthand syntax
var (
a = 1
b = 2
c = 3
) //3 using var(..) for declaring multiple variables with ease
The complete example of variable declaration can be seen in this code below.
package main
import (
"fmt"
)
var (
a = 1
b = 2
c = 3
) //declare multiple variables
func main() {
data := "Box this lap" //declare variable with shorthand syntax
fmt.Println(a, b, c) //print the value of a,b,c
fmt.Printf("%T %T %T\n", a, b, c) //print the type of a,b,c variable with %T notation
fmt.Println(data) //print the value of data
fmt.Printf("%s %T\n", data, data) //print the value with %s notation and the type with %T notation
}
The output of that code :
1 2 3
int int int
Box this lap
Box this lap string
The arithmetic operations are available in numeric types, the operand (the variable involved in the arithmetic operation) must have the same type. For example, the operation between the uint type and with int type cannot be executed because the type is different.
An example of an arithmetic operation can be seen in this code below
package main
import (
"fmt"
)
var a int = 10
var b int = 2
func main() {
d := a + b
e := a - b
f := a / b
g := a * b
h := a % b //gives the result of remainder between 10 and 2
fmt.Printf("The result of %d + %d equals %d\n",a,b,d)
fmt.Printf("The result of %d - %d equals %d\n",a,b,e)
fmt.Printf("The result of %d / %d equals %d\n",a,b,f)
fmt.Printf("The result of %d * %d equals %d\n",a,b,g)
fmt.Printf("The result of %d modulo %d equals %d\n",a,b,h)
}
The output of that code :
The result of 10 + 2 equals 12
The result of 10 - 2 equals 8
The result of 10 / 2 equals 5
The result of 10 * 2 equals 20
The result of 10 modulo 2 equals 0
Type Conversion
In Go, the value of a certain type can be converted to another type. To convert to the specified type using the destination type followed by "()" (to convert from string to a certain type use strconv.parseType() (example: strconv.parseInt("42", 10, 64)))
To convert int to float64, use the float64 type followed by "()"
a := 42
data := float64(a)
To convert string to int, use strconv package with Atoi() function.
a := "43"
data, _ := strconv.Atoi(a)
To convert int to string, use strconv package with Itoa() function.
a := 43
data := strconv.Itoa(a)
To learn more about strconv package, check this link
Sources
Here are some useful resources to learn more about the Go programming language
- best practice for writing Go code
- Go language specification
- Documentation about Go packages and third-party packages as well
I hope this article helps to learn the Go programming language. If you have any thoughts or feedback, you can write it in the comment section below.
Top comments (2)
I'm learning Golang too
Is it compulsory to always run the "go mod init project-name" before you start writing a program in Golang?
And also for the project name you used your Github link to the repository you created.. Is it compulsory to use that Github link for my project name?
Also I tried to follow the docs on the go.dev site and tried to install the rsc.io/quotes package but I wasn't able to install it, I was getting an error of "the package wasn't able to install itself"... I've tried to troubleshoot it a lot of times but to no avail...
What do you think I should do?
Sorry for the late reply,
you can use
go mod init project-name
to write Go codes outside the Go workspace directory. With go module (a.k.a. go mod), you can put the Go project in any possible folder.You can assign any name for the go module, it is not mandatory to use the GitHub link for the module name. This is an example:
For the
rsc.io/quotes package
problem. My solution is you can try to create a new Go project and then install the package. This is the complete walkthrough.main.go
file.Output
Hope it helps ;)