Data Types in "Go Language Tutorial" | Go Theme Month

Data Types in "Go Language Tutorial" | Go Theme Month

Hi everyone, my name is @

This article first appeared in the Los bamboo's official website

This article is translated from Golang tutorial series

This article is synchronized with the public account Luozhu Morning Tea House, please contact the author for reprinting.

Creating is not easy, forming a habit, three consecutive qualities!

The following 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
Means
boolean
Type, used to express
true
or
false
of.

Survival or destruction, this is a question!

package main import "fmt" func main () { 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 code

Run in playground

In the above program, the variable

a
Was assigned
true
,variable
b
Was assigned
false
.
c
Was assigned
a && b
The value of the expression. when
a
with
b
Are all
true
Time
&&
Operator returns
true
. Therefore the above variable
c
The value is
false
.

when

a
or
b
The value is
true
Time,
||
Operator returns
true
. In this example, because
a
The value is
true
, So the variable
d
The value is
true
. After the above program is executed, the following will be output:

A: to true B: to false C: to false D: to true copy the code

Signed integer

  • int8 : Represents an 8-bit signed integer
    • size : 8 bits
    • range : -128 to 127
  • int16 : Represents a 16-bit signed integer
    • size : 16 bits
    • range : -32768 to 32767
  • int32 : Represents a 32-bit signed integer
    • size : 32 bits
    • range : -2147483648 to 2147483647
  • int64 : Represents a 64-bit signed integer
    • size : 64 bits
    • range :-9223372036854775808 to 9223372036854775807
  • int : Represents a 32- or 64-bit integer, depending on the underlying platform. Unless you need to use an integer of a specific size, you should usually use
    int
    Represents an integer.
    • size : 32-bit in a 32-bit operating system, and 64-bit in a 64-bit operating system.
    • Range : -2147483648 to 2147483647 in 32-bit operating system, and -9223372036854775808 to 9223372036854775807 in 64-bit operating system
package main import "fmt" func main () { var a int = 89 b := 95 fmt.Println( "value of a is" , a, "and b is" , b) } Copy code

Run in playground

The above program will output

value of a is 89 and b is 95
.

In the above program,

a
Yes
int
Types of,
b
The type of is inferred from the value assigned to it (95). As mentioned above,
int
The size of is 32 bits in a 32-bit system and 64 bits in a 64-bit system. Let us proceed with verification.

can use

Printf
In the function
%T
Format specifier to print the type of the variable. Go has an unsafe package that has
Sizeof
Function, the function returns the size of the variable passed to it in bytes. Unsafe software packages 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 outputs variables

a
with
b
The type and size.
%T
Is the format specifier for the print type, and
%D
Used for printing size.

package main import ( "fmt" "unsafe" ) func main () { 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 code

Run in playground

The above program will output the following:

A 89 and IS of value IS B 95 type of int A IS, IS. 4 A size of type of int B IS, IS. 4 B size of duplicated code

We can infer from the above output

a
with
b
Is of type
int
, Their size is 32 bits (4 bytes). If you run the above program on a 64-bit system, the output will be different. In a 64-bit system,
a
with
b
Occupies 64 bits (8 bytes).

Unsigned integer

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

Floating point type

  • float32 : 32-bit floating point number
  • float64 : 64-bit floating point number

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

package main import ( "fmt" ) func main () { 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 code

Run in playground

a
with
b
The types of are inferred from the values assigned to them. under these circumstances,
a
with
b
Is of type
float64
. (
float64
Is the default type for floating-point values). We will
a
with
b
Add and assign it to a variable
sum
. We start from
a
Subtract
b
And assign it to
diff
. Then print out
sum
with
diff
. use
no1
with
no2
Perform similar calculations. The above program will print:

type of a float64 b float64 sum 14.64 diff -3.3000000000000007 sum 145 diff -33 Copy code

Complex type

Complex number is an extension of real number, which makes any polynomial equation have roots. --Wikipedia

  • complex64 : has
    float32
    Complex numbers with real and imaginary parts
  • complex128 : has
    float64
    Complex numbers with real and imaginary parts

Built-in function

complex
Used to construct complex numbers with real and imaginary parts.
complex
The function has the following definition:

FUNC Complex (R & lt, I FloatType) the ComplexType duplicated code

It takes real and imaginary parts as parameters and returns a complex number type. Both the real part and the imaginary part must be of the same type. which is

float32
or
float64
. If the real and imaginary parts are both
float32
, The return type of this function is
complex64
The plural value of. If the real and imaginary parts are both
float64
Type, this function returns
complex128
Complex value of type:

Plural numbers can be created using short declaration syntax:

C: = . 6 + 7i duplicated code

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

package main import ( "fmt" ) func main () { c1 := complex ( 5 , 7 ) c2 := 8 + 27i cadd := c1 + c2 fmt.Println( "sum:" , cadd) cmul := c1 * c2 fmt.Println( "product:" , cmul) } Copy code

Run in playground

In the above program,

c1
with
c2
Are two plurals.
c1
The real part of is 5 and the imaginary part is 7.
c2
It has a real part of 8 and an imaginary part of 27.
cadd
Is assigned as
c1
with
c2
The sum, while
cmul
Is assigned as
c1
with
c2
The product of. The program will output:

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

Other number types

  • byte :
    uint8
    Alias of
  • rune :
    int32
    Alias of

String type

In the Go language, a string is a collection of bytes. If this definition doesn't make any sense, it doesn't matter. Now, we can assume that the string is a collection of characters.

Let's use the string type to write a program:

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

Run in playground

In the above program, the string is first assigned

Naveen
, And finally assigned a string
Ramanathan
. can use
+
Operators concatenate strings together. for
name
Assigned
first
The value, which is concatenated to a space, followed by a
last
. The above program will output
My name is Naveen Ramanathan
.

There are also some operations that can be performed on strings. We will cover these in a separate tutorial.

Type conversion

Go is very strict about typing. 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 () { i := 55 //int j := 67.8 //float64 sum := i + j //int + float64 not allowed fmt.Println(sum) } Copy code

Run in playground

The above code is completely legal in C language. But in the case of go, this will not work.

i
Yes
int
Types of,
j
Yes
float64
Types of. We are trying to add 2 different types of numbers, which is not allowed. When you run the program, you will get
main.go: 10: invalid operation: i + j(mismatched types int and float64)
mistake.

To resolve the error,

i
with
j
Should have the same type. Let's move
j
Convert to
int
.
T(v)
Is the value
v
Convert to type
T
Syntax:

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

Run in playground

Now, when you run the above program, you will see

122
As output.

The same is true for distribution. An explicit type conversion is required to assign a variable of one type to another type. The following procedure can confirm this:

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

Run in playground

In line 9,

i
Convert to
float64
, And then be assigned to
j
. When you try to
i
Assigned to
j
Without any type conversion, the compiler will throw an error.