Hi, I’m @Luo Zhu

This article was first published on luo Zhu’s official website

This article was translated from Golang Tutorial Series

This article synchronizes in the public account Luo Zhu early teahouse, reprint please contact the author.

Creation is not easy, form a habit, quality three even!

Here are the basic types available in the Go language:

  • bool
  • Numeric Types
  • int8, int16, int32, int64, int
  • uint8, uint16, uint32, uint64, uint
  • float32, float64
  • complex64, complex128
  • byte
  • rune
  • string

bool

Bool Indicates a Boolean type, used to express true or false.

To be or not to be, that is the question!

package main

import "fmt"

func main(a) {
    a := true
    b := false
    fmt.Println("a:", a, "b:", b)
    c := a && b
    fmt.Println("c:", c)
    d := a || b
    fmt.Println("d:", d)
}
Copy the code

Run in playground

In the above program, variable A is assigned true and variable B is assigned false. C is assigned the value of the a && b expression. The && operator returns true if both a and b are true. So the value of variable C above is false.

When the value is true of a or b, | | operator returns true. In this example, because a is true, the variable d is true. The above program will output the following after execution:

a: true b: false
c: false
d: true
Copy the code

Signed shaping

  • int8: indicates an 8-bit signed integer
    • Size: 8 bits
    • Range: -128 to 127
  • int16: indicates a 16-bit signed integer
    • Size: 16
    • Range: -32768 to 32767
  • int32: indicates a 32-bit signed integer
    • Size: 32 bits
    • Range: -2147483648 to 2147483647
  • int64: indicates a 64-bit signed integer
    • Size: 64
    • Range: -9223372036854775808 to 9223372036854775807
  • int: represents a 32 – or 64-bit integer, depending on the underlying platform. You should usually use integers of a specific size unless you need themintRepresents an integer.
    • Size: the value is 32 bits for a 32-bit OS and 64 bits for a 64-bit OS.
    • Range: In a 32-bit operating system, the value ranges from -2147483648 to 2147483647. In a 64-bit operating system, the value ranges from -9223372036854775808 to 9223372036854775807
package main

import "fmt"

func main(a) {
    var a int = 89
    b := 95
    fmt.Println("value of a is", a, "and b is", b)
}
Copy the code

Run in playground

Value of a is 89 and b is 95.

In the above program, A is of type int and b’s type is inferred from the value assigned to it (95). As mentioned above, the size of int is 32 bits on 32-bit systems and 64 bits on 64-bit systems. Let’s go ahead and verify.

You can print the type of the variable using the % T format specifier in the Printf function. Go has an unsafe package, which has the Sizeof function, which returns the size, in bytes, of the variable passed to it. An insecure package should be used with caution because the code that uses it may have portability issues, but for the purposes of this tutorial, we can use it.

The following program prints the types and sizes of variables A and b. % T is the format specifier for printing type, and % d for printing size.

package main

import (
  "fmt"
  "unsafe"
)

func main(a) {
  var a int = 89
  b := 95
  fmt.Println("value of a is", a, "and b is", b)
  fmt.Printf("type of a is %T, size of a is %d", a, unsafe.Sizeof(a)) //type and size of a
  fmt.Printf("\ntype of b is %T, size of b is %d", b, unsafe.Sizeof(b)) //type and size of b
}
Copy the code

Run in playground

The above program will print the following:

value of a is 89 and b is 95
type of a is int, size of a is 4
type of b is int, size of b is 4
Copy the code

We can infer from the output above that a and B are of type int and have a size of 32 bits (4 bytes). If you run the above program on a 64-bit system, the output will be different. On 64-bit systems, A and B occupy 64 bits (8 bytes).

Unsigned shaping

  • uint8: represents an 8-bit unsigned integer
    • Size: 8 bits
    • Range: 0 to 255
  • uint16: represents a 16-bit unsigned integer
    • Size: 16
    • Range: 0 to 65535
  • uint32: represents a 32-bit unsigned integer
    • Size: 32 bits
    • Range: 0 to 4294967295
  • uint64: represents a 64-bit unsigned integer
    • Size: 64
    • Range: 0 to 18446744073709551615
  • uint: represents either a 32 – or 64-bit unsigned integer, depending on the underlying platform.
    • Size: 32-bit in a 32-bit system, 64-bit in a 64-bit system.
    • Range: The range is 0 to 4294967295 on a 32-bit system and 18446744073709551615 on a 62-bit operating system

Floating point types

  • Float32: a 32-bit floating-point number
  • Float64: a 64-bit floating point number

Here is a simple program to illustrate integer and floating point types:

package main

import (
 "fmt"
)

func main(a) {
  a, b := 5.67.8.97
  fmt.Printf("type of a %T b %T\n", a, b)
  sum := a + b
  diff := a - b
  fmt.Println("sum", sum, "diff", diff)
  no1, no2 := 56.89
  fmt.Println("sum", no1+no2, "diff", no1-no2)
}
Copy the code

Run in playground

The types of A and B are inferred from the values assigned to them. In this case, a and B are of type FLOAT64. Float64 is the default type for floating-point values. We add a and b and assign them to the variable sum. We subtract b from A and assign it to diff. Then print out sum and diff. Similar calculations are performed with NO1 and NO2. The above program will print:

typeOf a float64 B float64 sum 14.64 diff-3.3000000000000007 sum 145 diff-33Copy the code

Complex type

The complex number, an extension of the real numbers, gives roots to any polynomial equation. — Wikipedia

  • complex64:float32Complex numbers of real and imaginary parts
  • complex128:float64Complex numbers of real and imaginary parts

The built-in function complex is used to construct complex numbers with real and imaginary parts. The complex function has the following definition:

func complex(r, i FloatType) ComplexType
Copy the code

It takes real and imaginary parts as arguments and returns a complex type. Both the real and imaginary parts must be of the same type. That is float32 or float64. If both the real and virtual parts are float32, this function returns a complex value of type complex64. If both the real and virtual parts are of type FLOAT64, this function returns a complex value of type complex128:

Complex numbers can be created using short declarative syntax:

c := 6 + 7i
Copy the code

Let’s write a small program to help understand complex numbers:

package main

import (
 "fmt"
)

func main(a) {
  c1 := complex(5.7)
  c2 := 8 + 27i
  cadd := c1 + c2
  fmt.Println("sum:", cadd)
  cmul := c1 * c2
  fmt.Println("product:", cmul)
}
Copy the code

Run in playground

In the above program, c1 and c2 are two complex numbers. The real part of c1 is 5, and the imaginary part is 7. C2 has a real part 8 and an imaginary part 27. Cadd is assigned as the sum of C1 and c2, while CMUL is assigned as the product of C1 and c2. This program will output:

sum: (13+34i)
product: (-149+191i)
Copy the code

Other numeric types

  • byte:uint8The alias
  • rune:int32The alias

String type

In the Go language, a string is a collection of bytes. If the definition doesn’t make any sense, that’s fine. Now, we can assume that a string is a collection of characters.

Let’s write a program using the string type:

package main

import (
 "fmt"
)

func main(a) {
  first := "Naveen"
  last := "Ramanathan"
  name := first +""+ last
  fmt.Println("My name is",name)
}
Copy the code

Run in playground

In the above program, the string Naveen is assigned first, and the string Ramanathan is assigned last. Strings can be concatenated using the + operator. Name is assigned the value of first, which is concatenated to a space followed by a last. The above program will print My name is Naveen Ramanathan.

There are also operations you can perform on strings. We’ll cover these in a separate tutorial.

Type conversion

Go is very strict about type specificity. There is no automatic type promotion or conversion. Let’s look at an example to understand what this means:

package main

import (
 "fmt"
)

func main(a) {
  i := 55 //int
  j := 67.8 //float64
  sum := i + j //int + float64 not allowed
  fmt.Println(sum)
}
Copy the code

Run in playground

The above code is perfectly legal in C. But in the case of GO, this won’t work. I is of type int and j is of type float64. We are trying to add 2 different types of numbers, which is not allowed. When running the program, you will get main.go: 10:invalid operation: I + j(mismatched types int and float64).

To resolve this error, I and j should have the same type. Let’s convert j to int. T(v) is the syntax for converting a value v to type T:

package main

import (
 "fmt"
)

func main(a) {
  i := 55 //int
  j := 67.8 //float64
  sum := i + int(j) //j is converted to int
  fmt.Println(sum)
}
Copy the code

Run in playground

Now, when you run the above program, you will see 122 as output.

The same is true of distribution. Explicit type conversions are required to assign a variable of one type to another. The following procedure confirms this:

package main

import (
 "fmt"
)

func main(a) {
  i := 10
  var j float64 = float64(i) //this statement will not work without explicit conversion
  fmt.Println("j", j)
}
Copy the code

Run in playground

In line 9, I is converted to FLOAT64 and then assigned to J. The compiler raises an error when you try to assign I to J without any type conversion.