Go is an open-source programming language that makes it easy to build simple, reliable, and efficient software. In this article, I’ll make sure you get the fundamentals of the Go Programming Language.
Getting Started
First, we need to install the Go programming language. You can do that by clicking this link.
How to write code for Go
Go programs are organized into packages. A package is a collection of source files in the same directory that are compiled together. Functions, types, variables, and constants defined in one source file are visible to all other source files within the same package.
— How to Write Go Code — The Go Programming Language. https://golang.org/doc/code.html
To compile and run a simple program, choose a module path, and create a ‘go .mod’ file that declares it:
$ mkdir go-project
$ cd go-project
$ go mod init go-project
go: creating new go.mod: module go-project
$ cat go.mod module go-project
$
Next, create a file named app.go inside that directory containing the following Go code:
package main
import "fmt"
func main() {
fmt.Println("Hello Medium")
}
Now you can install that program with the go
tool:
$ go install go-project
This command builds the go-project command, producing an executable binary.
Source Control
If you’re using a source control system, now would be a good time to initialize a repository, add the files, and commit your first change. Again, this step is optional: you do not need to use source control to write Go code.
— How to Write Go Code — The Go Programming Language. https://golang.org/doc/code.html
$ git init
Initialized empty Git repository in go-project/.git/
$ git add go.mod app.go
$ git commit -m "initial commit"
Importing Packages
An import can describe how to obtain the package source code using a revision control system such as Git.
package main
import (
"fmt"
"github.com/google/go-cmp/cmp"
)
func main() {
fmt.Println(cmp.Diff("Hello World", "Hello Go"))
}
When you run commands like ‘go install,’ ‘go build,’ or ‘go run,’ the ‘go’ command will automatically download the remote module and record its version in your ‘go.mod’ file.
Variabele Declaration
Inside a function, the ‘:=’ short assignment statement can be used in place of a ‘var’ declaration with implicit type.
package mainimport "fmt"func main() {
var i, j int = 1, 2
k := 3
c, python, java := true, false, "no!"fmt.Println(i, j, k, c, python, java)
}
As in other programming languages, Go contains the following data types:
- String — A string is a sequence of characters
- Integer — An integer data type is a non-decimal number
- Float — A float is a number with a decimal point or a number
- Boolean — A Boolean represents TRUE or FALSE.
- Array — An array stores multiple values in one single variable.
Basic Operators
- + (Addition)
- – (Subtraction)
- * (Multiplication)
- / (Division)
- % (Modules)
- ** (Exponentiation)
Arrays
Go programming language provides a data structure called the array, which can store a fixed-size sequential collection of elements of the same type.
— Go — Quick Guide — Tutorialspoint. https://www.tutorialspoint.com/go/go_quick_guide.htm
Declaring an array
var variable_name [SIZE] variable_type
Initializing an array
var balance = [2]float32{100.22, 500.03}
Loops
A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages.
— Arduino — Loops — Tutorialspoint. https://www.tutorialspoint.com/arduino/arduino_loops.htm
For loop
The following code executes an infinite loop:
package main
import "fmt"
func main() {
for true {
fmt.Printf("This loop will run forever.\n");
}
}
Recursion
In the most simple form, a recursive function is a function that calls itself:
func recursion() {
recursion() /* function calls itself */
}
func main() {
recursion()
}
Programs that can be written with recursive functions are for example a function to calculate the factorial:
package main
import "fmt"
func factorial(i int)int {
if(i <= 1) {
return 1
}
return i * factorial(i - 1)
}
func main() {
var i int = 15
fmt.Printf("Factorial of %d is %d", i, factorial(i))
}
Conclusion
After this article, I hope you can write a simple go code now and have a good idea of what Go is about.