Golang Tutorial Part #6 ~ Data Types

Mantan Programmer
Towards Dev
Published in
4 min readApr 28, 2022

--

Hello, how are you all friends? This time we will continue the Golang tutorial, namely the Data Type in Golang. Golang has several data types numeric, string, and boolean. In the previous discussion, we have implemented several data types such as string and numeric. In part 6, we will explore the golang data type.

A. Non-Decimal Numerical Data Type

The data type has two functions, as decimal and non-decimal. this time we will discuss non-decimal numeric data

  • uint, Data type for fractional numbers
  • int, Data type for integer

The two data types above are then further divided into several types, with the division based on the width of the value range, the details can be seen in the following table.

Example :

// Go program to illustrate
// the use of integers
package main
import "fmt"

func main() {

// Using 8-bit unsigned int
var X uint8 = 225
fmt.Println(X, X-3)

// Using 16-bit signed int
var Y int16 = 32767
fmt.Println(Y+2, Y-2)
}

Output

225 222
-32767 32765

B. Float Data Type

Next we will get to know Float in golang, the Float data type is still in the Integer category. Floating-Point Numbers: In the Go language, floating-point numbers are divided into two categories as shown in the table below:

float3232-bit IEEE 754 floating-point number

float6464-bit IEEE 754 floating-point number

Example :

// Go program to illustrate
// the use of floating-point
// numbers
package main
import "fmt"

func main() {
a := 20.45
b := 34.89

// Subtraction of two
// floating-point number
c := b-a

// Display the result
fmt.Printf("Result is: %f", c)

// Display the type of c variable
fmt.Printf("\nThe type of c is : %T", c)
}

Output :

Result is: 14.440000
The type of c is : float64

C. Complex Data Types

Complex data type: Complex is divided into two parts as shown in the table below. float32 and float64 are also part of this complex number. The built-in functions create complex numbers from their imaginary and real parts and the built-in imaginary and real functions extract those parts.

complex64: Complex numbers which contain float32 as a real and imaginary component.

complex128: Complex numbers which contain float64 as a real and imaginary component.

Example :

// Go program to illustrate
// the use of complex numbers
package main
import "fmt"

func main() {

var a complex128 = complex(6, 2)
var b complex64 = complex(9, 2)
fmt.Println(a)
fmt.Println(b)

// Display the type
fmt.Printf("The type of a is %T and "+
"the type of b is %T", a, b)
}

Output:

(6+2i)
(9+2i)
The type of a is complex128 and the type of b is complex64

D. Data Type bool (Boolean)

The boolean data type has two values, namely true and false. This data type is usually used to select conditions and loops.

Example :

// Go program to illustrate
// the use of booleans
package main
import "fmt"

func main() {

// variables
str1 := "TemanNgoding"
str2:= "temanNgoding"
str3:= "TemanNgoding"
result1:= str1 == str2
result2:= str1 == str3

// Display the result
fmt.Println( result1)
fmt.Println( result2)

// Display the type of
// result1 and result2
fmt.Printf("The type of result1 is %T and "+
"the type of result2 is %T",
result1, result2)

}

Output:

false
true
The type of result1 is bool and the type of result2 is bool

E. String Data Type

String data type, which has a text value or letters, usually string values will have a sign (“) at the beginning and end of the text. In addition to using quote marks, string declarations can also be made with grave accent/backticks (`), this mark is located to the left of the 1 key. The specialty of strings declared using backtics is that all characters in it are not escaped, including \n, quotation marks. two and single quotes, newlines, and so on. All will be detected as strings.

Example:

// Go program to illustrate
// the use of strings
package main
import "fmt"

func main() {

// str variable which stores strings
str := "TemanNgoding"

// Display the length of the string
fmt.Printf("Length of the string is:%d",
len(str))

// Display the string
fmt.Printf("\nString is: %s", str)

// Display the type of str variable
fmt.Printf("\nType of str is: %T", str)
}

Output:

Length of the string is:12
String is: TemanNgoding
Type of str is: string

F. Value nil & Zero Value

Nil is an empty value, completely empty. nil cannot be used with the data types discussed above. There are several data types that can be set to nil, including:

  • pointer
  • tipe data fungsi
  • slice
  • map
  • channel
  • interface kosong atau interface{}

Later for the next tutorial we will discuss the value of nil in golang.

That’s the tutorial that I can convey, hopefully it will be useful for friends at will. Send regards for success…

Thank you…

Don’t forget to read the previous tutorial as well.

Golang Tutorial Part #5 ~ Golang Variables

Golang Tutorial Part #4 ~ First Program : Hello Word

Golang Tutorial #3 : Work Environment and Terms in Golang

Golang Tutorial #2 : Installing Golang on Linux, Windows and Mac OS

Golang Tutorial #1 : Getting acquainted with Golang Bahasa

--

--