Golang Introductory article-Basic type

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Golang has the following basic types:

    • Integer types: int8, int16, etc.
    • Floating-point types: float32, float64
    • Boolean type: BOOL
    • Plural type: complex64, complex128
    • String Type: String
    • Character type: Byte, rune

Integer type

Integer conversions

The compiler in Golang does not do an implicit type conversion, and int and int32 are two different types.

varint3264           // value1将会被自动推导为 int类型value2 = value1        // 编译错误int32// 强制类型转换编译通过

When you do coercion type conversions, you need to be aware of the loss of data accuracy (such as forcing floating-point numbers to integers) and value overruns (when values exceed the range of values of the target type of the conversion) that occur when the data length is truncated.

Integral type operation

Golang supports general integer operations: +-*/% is the subtraction modulo operation.

Comparison of integral types

Golang support for general comparison operators: > < = >= <=! =
It is important to note that two different integer type variables cannot be directly compared, such as the int8 type variable and the INT type variable cannot be directly compared, but the integer constant compiler automatically infers the type, so the integer constant can be compared with any integral type variable.

varint8varint16x = 1y = 1if x == y {                 // 编译错误    fmt.Println("x == y")}if x == 1 &&  y == 1 {      // 编译通过    fmt.Println("x == y")}

Bit arithmetic

Boolean type

Boolean type in Golang, bool keyword, true and false for predefined values
The bool type does not support automatic and forced type conversions, and can only be assigned through predefined values, as well as by comparison expression derivation.

varbooltruev2 := (12)    // v2也会被推导为 bool类型,值为false1            // 编译错误不支持隐式类型转换bool(1)      // 编译错误不支持强制类型转换v1 = (12)     // 编译成功,值为false

Floating-point types

Floating-point types in Golang are expressed in the IEEE-754 standard.

Floating-point type conversions

Floating-point types in Golang have two float32 and float64, float and double in the same C language.

varfloat32f1 = 1f2 := 1.0           //  如果想让f2被推导成浮点型,必须加.0, 否则被推导为整型而不是浮点型f1 = f2             //  编译错误,浮点型会被自动推导成float64float32(f2)    //  编译成功

Floating-point type comparisons

The comparison of floating-point numbers need to pay attention to the problem of precision, like integer type directly with = = To determine whether the two floating point is not feasible,
Can produce unstable results, so it is feasible to implement a comparison function for a floating-point number.

import"math"funcfloat64bool {    // p为用户自定义的比较精度,小于比较精度就可认为相等    return math.Fdim(f1, f2) < p   }

Plural type

A complex number is made up of two real numbers (floating point numbers), a real part (real), and an imaginary part (image). The real part of the complex can be obtained through the function real (v), and the function image (v) obtains the imaginary part of the complex number.

varcomplex64        //  由2个float32构成的复数类型v1 = 1.2 + 12iv2 := 1.2 + 12i         //  v2 是complex128类型complex(1.2, 12)  //  v3 等于 v2real(v1)           //  x值为1.2real(v1)           //  y值为12

Character type

Two character types are supported in Golang, one is a byte (which is actually an alias of Uint8), a value representing a single byte of the UTF-8 string, and the other is Rune, which represents a single Unicode character. The Unicode/utf8 package also provides a conversion between UTF8 and Unicode.

String manipulation

The string in Golang is also a basic type, which is very handy compared to C + + and Golang strings.
Initialization

varstring         // 声明一个字符串变量"Hello world"    // 字符串赋值ch := str1[0]           // 取字符串的第一个字符"Hello world"   // 直接初始化,推导为string类型str2[0'a'           // 编译错误,不支持初始化后修改内容str1 = str1 + str2      // str1的值变为了Hello worldHello world

The content of a string is similar to an array, and is supported by subscript methods. Different arrays can modify the content, and the content cannot be modified after the string is initialized. Strings support connection operations, implemented with the + operator.

String traversal

There are two ways to traverse a string, one is to traverse by Byte character, and one is to traverse by rune character.

//按byte字符遍历str"Hello, 世界"for0; i < len(str); i++ {    // 利用下标取字符串中的字符,类型为 byte    str[i]    fmt.Println(i, ch)}//按rune字符遍历str"Hello, 世界"forstr {    // ch的类型为 rune,i为rune的索引下标    fmt.Println(i, ch)}

Golang Technology Exchange QQ Group, welcome like-minded people together to explore the technical life

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.