DEV Community

Cover image for Learning Golang • #Issue2
Tabintel
Tabintel

Posted on

Learning Golang • #Issue2

This is a documentation of the concepts I learnt from the Golang course on Sololearn.

Go is an open source programming language that makes it easy to build simple, reliable, and efficient software. Go was designed at Google in 2007 to improve programming productivity in an era of multicore, networked machines and large codebases.

package  main
import   "fmt"

func main () {
   fmt.Println ("Go is fun to learn!")
} 

Enter fullscreen mode Exit fullscreen mode

Packages: Every Go program is made up of packages. A Go program starts running in the main package. That is why we need to declare our code as the main package - to make it run and create the output:

Apart from main, Go has many packages that can be imported and used in the code to accomplish different tasks. One of the most popular packages is “fmt”, which stands for format, and provides input and output functionality.

Imports: Each package has exported names, which you can access after importing them. In Go, a name is exported if it begins with a capital letter. You can access the exported names using the: Package name, a dot, and the exported name.
We access the PrintIn () function of the fmt package, which is used to generate the output. We provide the output we want to generate inside the parentheses enclosed in quotes.
Similar to other programming languages such as Java or C++, func main () is the entry point of our program. It is the function that gets executed when we run our program.
Below is a code that generates a text output:

package main
import  "fmt"

func main () {
   fmt.Println ("this is a test case!")
}
Enter fullscreen mode Exit fullscreen mode

We can see that the code:
Defines the main package
Imports the fmt package used for output
Defines the main () function
Uses the Println () function of the fmt package to generate the desired output.

Comments: Comments are statements in the code that explains what the code is doing. In Go, you can create a single line comment using two slashes //. Anything that follows the slashes is considered a comment. Example:

// generate the output
    fmt.Println ("I code and build")

Enter fullscreen mode Exit fullscreen mode

Writing comments is a good practice and it allows you and other developers to easier read and understand what the code is doing.
We can also create multi-line comments in Go! To do this we put the comment inside /* and */.
Example:

func main () {
/*
Developing solutions and building effective models to ensure adequate frameworks are used.
*/
    fmt.Println("Hey what's up")
}
Enter fullscreen mode Exit fullscreen mode

Variables: Variables are used to store values. In Go, the var keyword is used to declare variables. Example:


 var x int = 8
 fmt.Println (x)
Enter fullscreen mode Exit fullscreen mode

The code above declares a variable named i of type int. (int stands for integers and represents whole numbers. We can assign the variable a value and output it. We can also declare multiple variables on one line and assign values to them.

var i , k = 6, 79
Enter fullscreen mode Exit fullscreen mode

If you assign a value to the variable you can omit the type declaration, as Go will
automatically take the type of the value assigned.

var i, j = 4, 56
Enter fullscreen mode Exit fullscreen mode

The process of assigning values to variables is called “initialization”. Go supports short variable declaration using :=
Example: i := 8
It can also be used to declare and initialize multiple variables on one line: x, y := 17, 8
The := operator automatically defines and initializes variables with the given value.

Data Types
Common types that Go Supports:

Float 32 - a single precision floating point value
Float 64 - a double-precision floating point value
String - a text value
Bool - boolean true or False

The difference between float32 and float64 is the precision, meaning that float64 will represent the number with higher accuracy, but will take more space in memory.

Examples of Data types in code:


var k int = 30
var r float32 = 2.43
var name string = "Fiona"
var online bool = false

fmt.Println(name)
fmt.Println(k)
fmt.Println(r)
fmt.Println(online)
Enter fullscreen mode Exit fullscreen mode

Another interesting feature of Go are zero values: variables that are declared without a value, takes the zero value of their type:


// 0 for numeric types
// false for the boolean type
// "" for strings
Enter fullscreen mode Exit fullscreen mode

Constants: A variable can change its value during the program. Example:

var b = 15
b = 2
Enter fullscreen mode Exit fullscreen mode

In some cases, your program may need values that are preserved during the program. These are called constants and they cannot be changed from their initial value. Constants are declared like variables, but with the const keyword and needs to be assigned a value.

const pi = 3.14 
Enter fullscreen mode Exit fullscreen mode

[ pi is a constant and cannot be changed] Constants cannot also be declared using the := syntax
Arithmetic Operators: Go supports all of the common arithmetic operators. The process of adding together strings is called string concatenation.
Examples:


a := 10
b := 9

// Addition
res := a + b
fmt.Println(res)

// Subtraction
res = a - b
fmt.Print(res)

// Multiplication
res = a * b
fmt.Println(res)

// Division
res = a / b
fmt.Println(res)

// Modulus, it results in the remainder of the division
res = a % b
fmt.Println(res) 
Enter fullscreen mode Exit fullscreen mode

The plus operator + can also be used to add together strings. Example:


q := "happy"
g := "everyday"
fmt.Println(q + g)
Enter fullscreen mode Exit fullscreen mode

Assignment Operators

Go also supports short assignment operators, which allow you to replace statements such as:
a = a + b with a shorter version: a += b
There are also short assignment operators for other arithmetic operators, such as subtraction, division, modulo.


a := 35
b := 9
a += b
fmt.Println(a)

a *= b
fmt.Print(a)


Enter fullscreen mode Exit fullscreen mode

Discussion (0)