Golang Learning notes-1.4 data types

Source: Internet
Author: User
Tags byte sizes diff

This is the fourth chapter of Golang Language Learning Tutorial
In the go language, data types are used to declare functions and variables.
The go language has the following data categories by category

Serial Number type Description
1 bool The bool type represents a Boolean value of true or false
2 Number Type Integer int and floating-point float32, Float64. Go supports integer and floating-point numbers, and the native support complex number.
3 String type A sequence of fixed-length characters connected by a single byte in the Go language, using UTF-8 encoding to identify Unicode text.
4 Derived types Including:
(a) pointer type (Pointer)
(b) Array type
(c) structured type (struct)
(d) Channel type
(e) Function type
(f) Slice type
(g) interface type (interface)
(h) Map type

bool

The bool type represents a Boolean value of true or false
Cases:

package main import . "fmt"func main(){        a := true //定义 a为true        b := false //定义 b为false        Println("a is",a, "b is",b)        c := a && b //c = a 和 b做与运算        Println("c is",c)        d := a || b //d = a 和 b做或运算         Println("d is",d)}

The results of the above program operation are as follows:

A is true B is false
C is False
D is True

Number Type

ordinal type and description
1 uint8
Unsigned 8-bit integer (0-255)
2 uint16
Unsigned 16-bit integer (0-65535)
3 uint32
Unsigned 32-bit integer (0-42949672
4 UInt64
Unsigned 64-bit integer (0-18446744073709551615)
5 int8
signed 8-bit integer ( -128-127)
6 in T16
Signed 16-bit integer ( -32768-32767)
7 int32
signed integer 32-bit (-2147 483648-2147483647)
8 Int64
signed integer 64 bits (-9223372036854775808-92 23372036854775807)

int: represents a 32 or 64-bit integer based on a different underlying platform (underlying Platform). Unless you have a specific need for the size of an integral type, you should typically use int to represent an integer type.
Size: 32 bits under 32-bit systems, and 64 bits under 64-bit systems.

Cases:

package mainimport (        "fmt"        "unsafe")func main(){        a,b := 95,80 //给 a 和 b 赋值        fmt.Println("values of a is",a, "values of b is",b)        fmt.Printf("the type of a is %T,the size of a is %d\n",a, unsafe.Sizeof(a))  //输出 a 的类型和类型大小        fmt.Printf("the type of b is %T,the size of b is %d\n",b, unsafe.Sizeof(b)) //输出 b 的类型和类型大小,\n换行}

In the Printf method, you can print out the type of the variable by using the %T format specifier (formatted specifier). The unsafe package for Go provides a Sizeof function that receives the variable and returns its byte size. The unsafe package should be used with caution because the use of unsafe packets can introduce portability problems. However, for the purposes of this tutorial, we can use it.

The above program outputs the type and size of variables A and B. The format specifier %T is used for printing types and %d for printing byte sizes.

The results of the above program operation are as follows:

Values of a is the values of B is 80

The type of a is int,the size of a is 8
The type of B is int,the size of B is 8

From the above output, we can infer that A and B are of type int, and the size is 64 bits (8 bytes).

Floating point Type

package mainimport "fmt"func main(){        a,b := 11.5,55.98 //自动推断 a, b 的类型        fmt.Printf("Type of is a is %T, type of b is %T\n",a,b) //输出 a, b 的类型        sum := a+b //将a+b赋值给sum        diff := a-b //将a-b赋值给diff        fmt.Println("sum",sum, "diff",diff) //输出sum、diff}

The above procedure simply demonstrates the use of floating-point type.
The results of the operation are as follows:

Type of is a-float64, type of B is float64
Sum 67.47999999999999 diff-44.48

Plural type

The go language provides two precision complex types: complex64 and complex128, respectively, corresponding to float32 and float64 two floating point precision.
The built-in complex function is used to construct complex numbers, and built-in real and IMAG functions return the real and imaginary numbers of complex numbers, respectively.
Cases:

package mainimport ( "fmt" )func get_real_imag() {        value4 := 1.2 + 10i        var r = real(value4) //返回复数的实部: 1.2        var i = imag(value4) //返回复数的虚部: 10        fmt.Println("the real is",r, "and the imag is",i)}func main() {        get_real_imag()}

The results of the above operation are as follows:

The real is 1.2 and the IMAG is 10

Type conversions

Go has very strict strong-type characteristics. Go does not have automatic type promotion or type conversion.
Cases:

package main  import "fmt"func main(){        i := 1 // i 的类型为int        j := 5.5  // j 的类型为float64        sum := i + j //赋值sum为 i + j        fmt.Println(sum)}

The above procedure will be error:

i + j (mismatched types int and float64)

Trying to add a number of two different types is not possible, so I need to convert I to float64 type
Cases:

package main  import "fmt"func main(){        i := 1 // i 的类型为int        j := 5.5  // j 的类型为float64        sum := float64(i) + j //赋值sum为 i + j,转换 i 的类型为float64        fmt.Println(sum)}

The results of the above operation are as follows:

6.5

Note: This example converts I to float64, and the operation is normal. If J is converted to an int integer type, j becomes an integer and the output is 6.

String type (String)

In Golang, a string is a collection of bytes.
Cases:

package main  import "fmt"func main(){        first := "Hello,everyone!" //first 赋值        second := "This is ergouzi's Mac " //second 赋值        all := first + " " + second // + 操作符用于拼接字符,将first、空格、second拼接在一起赋值给all        fmt.Println(all)}

The results of the above operation are as follows:

hello,everyone! This is Ergouzi ' s MAC

Above for learning Golang data type notes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.