programming

Getting to know Data Types in Golang

##Numbers

Integers

There are integer types that we can use, including uint8, uint16, uint32, uint64, int8, int16, int32 and int64. The following is an example of using integer type variables in the go program:

package main

import "fmt"
func main() {
    fmt.Println("1 + 1 =", 1 + 1)
}

If you run this program and you can see the output as below:

$ go run main.go 
1+1=2

From this program we will print the string in the form of 1 + 1= and the operation 1 + 1 in arithmetic which will operate the addition, namely with the result 2.

Floating

There are float types that we can use, including float32, float64, complex64 and complex128.

What if we change the operation to 1.0 + 1.0? Will the results of these arithmetic operations be the same?

Of course it will be different because go defines .0 as the float data type.

Apart from that, we also need to know that Go has several basic arithmetic operations as follows:

xOperations
+summation
-subtraction
*multiplication
/division
%modulo

Strings

If we want to print string there are 2 ways, namely by using single quotation marks and double quotation marks. For example, Hello World and "Hello World" can be used. To add a new line to the string to be printed we can add \n and for tab character we can add \t.

Let’s try modifying the hello-world.go program into a program like the one below:

package main

import "fmt"

func main() {
	fmt.Println(len("Hello World"))
	fmt.Println("Hello World"[1])
	fmt.Println("Hello " + "World")
}

Some things to pay attention to:

  • Spaces in the string will be counted as 1 character so that on line 1 it will produce 11 where the len function is used to find out the number of characters in the string
  • The string starts index from 0 not from 1. [1] looks for the 2nd element of Hello World, namely the character e. Note that what the program line 2 prints is not e but 101 when running this program. Remember! the program will print characters represented by bytes and byte is an integer. (The ASCII of the letter e is 101)
  • Merger uses the same symbols as addition. The Go compiler figures out what to do based on the argument type. Since + is a string, the compiler assumes we mean concatenation and not addition.

Boolean

Boolean represents bits which have the value of 1 integer bit which is represented as true and false. There are 3 logical operations that we can use, namely:

xOperations
&&and
||or
!note

Below is an example of this program so you can understand better.

package main

import "fmt"

func main() {
    fmt.Println(true && true)
    fmt.Println(true && false)
    fmt.Println(true || true)
    fmt.Println(true || false)
    fmt.Println(!true)
}

After running this program we will see output like this

$ go run boolen.go
true
false
true
true 
false

We can also see the and logic table below

ExpressionValue
true && truetrue
true && falsefalse
false && truefalse
false && falsefalse

Tabel logic or

ExpressionValue
true or truetrue
true or falsetrue
false or truetrue
false or falsefalse

Tabel Logic not

ExpressionValue
!truefalse
!falsetrue

comments powered by Disqus