Go Language Summary (2)--numeric type

Source: Internet
Author: User
Tags mathematical functions

The Last Post summarizes the basics of Go Language--go language Summary (1)--basic knowledge, this article

First, the integral type

The go language has 13 kinds of shaping, of which 2 are just different names, the essence is the same, so, in essence go language has 11 kinds of shaping. As follows:

(1) Int: Depending on the implementation of different platforms, it can be int32 or int64

(2) Int8: ( -128-127)

(3) Int16: ( -32768-32767)

(4) Int32: ( -2 147 483 648, 2 147 483 647)

(5) Int64: ( -9 223 372 036 854 775 808, 9 223 372 036 854 775 807)

(6) Unit: Depending on the implementation of different platforms, can be int32 or int64

(7)unit8 (aka Byte): (0-255)

(8) Unit16: (0-65535)

(9)Unit32 (aka Rune): (0, 4 294 967 295)

(0) Unit64: (+ 18 446 744 073 709 551 615)

(one)unitptr : The type that fits the pointer value exactly, on the 32-bit platform is unit32, on the 64-bit platform is Unit64

(PS: It is important to note that there is no automatic type conversion in the Go language, which means that, in addition to the comparison operators of the previous post analysis, the other operators basically need type conversions in order to operate.) Otherwise, it is a compilation error)

Second, floating-point type

The go language has 2 types of floating-point and two types of complex types.

(1) float32 ( -3.402...x1038-3.402...x1038)

(2) float64 ( -1.797...x10308-+1.797...x10308)

(3) complex64 (real part, imaginary part is a float32)

(4) complex128 (real part, imaginary part is a float64)

(PS: The standard library math package contains a number of mathematical functions, all of which use float64,

The standard library MATH/CMPLX contains numerous complex-related mathematical functions, all of which use complex128)

(PPS: As in mathematics, the Go language uses the suffix I to denote complex numbers, such as 5 + 5.1i)

Three, string

The go language string is a variable-width sequence of characters encoded with UTF-8, with each character represented by one or more bytes. This differs from Java in that Java is represented by UTF-16, which corresponds to 2 bytes per character.

(1) Create: One is enclosed in double quotation marks ("), which represents a resolvable string that can be escaped by character. The other is enclosed in single quotation marks ('), which represent the original string, which can contain any character except the anti-quote, and can of course be wrapped. as follows: 

func test1 () {    str1:"\" It ' s me!\ ""    str2:= '"it ' s Me,too"'    fmt. Println (str1)    FMT. Println (STR2)}

The output is:

" it ' s me! " " it ' s Me,too "

(2) The Go language string supports the "+ =" operation, you can get the original byte at the index by [n], [n:m], [n:], [: M] to get the byte corresponding to the string, if the character is truncated, display garbled. Such as:

 func test1 () {str1:  =  " go languages is a sequence of variable width characters encoded with UTF-8, with each character represented by one or more bytes.    FMT. Println (str1[ 4 : 15   5 : 15   ]) fmt. Println (str1[:  ]) fmt. Println (Len (str1))  //  FMT.P Rintln (Len ([]rune (STR1)) // } 

The output is:

 Word of speech? 232 Go Language 109  A

(3) The string supports the general comparison operator operation, comparing the operator in memory with a byte-by-byte comparison string. Of course, this comparison is based on the order of UTF-8 encoding.

(4) The last number of characters in the previous example:len ([]rune (STR1)), in the Go language, a string can be represented by a Rune (aka Int32) array, each rune represents a single character. Such as:

func test1 () {    str1:"The string of thego language is a sequence of variable width characters encoded with UTF-8, each of which is represented by one or more bytes.  for    char : = range []rune (str1) {        fmt. Println (char)    }}

This operation will print out each character number of the str1 directly

Iv. pointers

(1) The nature of the pointer, with a simple example to illustrate:

func test2 () {A:="XYZ"B:="OPQ"PA:= &a//PA is a pointer to aPP: = &pa//pp is a pointer to the PAFmt. Println (A, B, *PA, * *pp) a+="ZZ" //a append "zz"Fmt. Println (A, B, *PA, * *pp)*pa + ="BB" //pp point value, append "BB"Fmt. Println (A, B, *PA, * *pp) fmt. Println ("Print a various cases:", &A, a) fmt. Println ("Print PA In various cases:", &PA, PA, *PA) fmt. Println ("Print pp in various cases:", &PP, pp, *pp, * *pp)}

The output is as follows:

0xc0820001d00xc0820380180xc0820001d0 0xc082038020  0xc0820380180xc0820001d0 XYZZZBB

It can be seen that the essence of a pointer is a variable that holds a logical address.

(2) There are two syntax for creating variables, along with pointers to them: new (Type) and &type{}, as follows:

struct {    int    int}func test3 () {A: = point{1 2      }    B:new(point)    c:= &point{}    d:= &point{ 3 4 }    FMT. Println (A, B, C, D)}

The output is:

{12} &{00} &{00} &{34}

When the go language prints a pointer to a struct, it prints the contents of the structure after the solution is referenced, and the & is prefixed to indicate that it is a pointer.

Go Language Summary (2)--numeric type

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.