##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:
x | Operations |
---|---|
+ | 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 thelen
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 ofHello World
, namely the charactere
. Note that what the program line 2 prints is note
but101
when running this program. Remember! the program will print characters represented by bytes andbyte
is an integer. (The ASCII of the lettere
is101
) - 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:
x | Operations |
---|---|
&& | 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
Expression | Value |
---|---|
true && true | true |
true && false | false |
false && true | false |
false && false | false |
Tabel logic or
Expression | Value |
---|---|
true or true | true |
true or false | true |
false or true | true |
false or false | false |
Tabel Logic not
Expression | Value |
---|---|
!true | false |
!false | true |