Golang Go Basic Type

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

Boolean

Boolean value of type Bool,true or FALSE, default false

    var isActive bool  // 全局变量声明    var enabled, disabled = true, false  // 忽略类型的声明    func test() {        var available bool  // 一般声明        valid := false      // 简短声明        available = true    // 赋值操作    }

Numeric type

Integer type

An integer type has unsigned and signed two types. go supports both int and uint, both types are the same length, but the exact length depends on the implementation of the different compilers .
Go also has a direct definition of the type of the number of digits:
Rune, Int8, Int16, Int32, Int64 and Byte, Uint8, UInt16, UInt32, UInt64.

Integer

    • int8 ( -128-127)
    • Int16 ( -32768-32767)
    • Int32 ( -2,147,483,648-2,147,483,647)
    • Int64 ( -9,223,372,036,854,775,808-9,223,372,036,854,775,807)

unsigned integer:

    • Uint8 (0-255)
    • UInt16 (0-65,535)
    • UInt32 (0-4,294,967,295)
    • UInt64 (0-18,446,744,073,709,551,615)

其中rune是int32的别称byte是uint8的别称

Floating point number

There are two types of floating-point numbers float32 and float64 (no float type), and the default is float64. (IEEE-754 Standard)
You should use float64 as much as possible, because all the math-related functions in the math package will require receiving this type.

    • float32 (+-1e-45 +-3.4 * 1e38)
    • Float64 (+-5 1e-324-107 1e308)

Plural

The default type is complex128 (64-bit real number + 64-bit imaginary number). If smaller, there are also complex64 (32-bit real number + 32-bit imaginary numbers).
The plural form is re + IMi, where re is the real part, IM is the imaginary part, and the last I is the imaginary unit.

var c1 complex64 = 5 + 10ifmt.Printf("The value is: %v", c1)// 输出: 5 + 10i

Format specifier

In the formatted string,
%d is used to format integers (%x and%x are used to format a number of 16 binary representations),
%g is used to format floating-point types (%f output floating-point numbers,%e output scientific notation notation),
The%0d is used to specify an integer that outputs a fixed length, where the first digit 0 is required.

%N.MG is used to represent the number n and is accurate to the M-bit after the decimal point, in addition to using G, you can also use E or F, for example: using the formatted string%5.2e to output 3.4 of the result is 3.40e+00.

Strings string

A string is a sequence of characters concatenated with fixed-length characters. The string of Go is connected by a single byte. That is, the traditional string is made up of characters, and go's string is different, it is composed of bytes.

The byte of the Go language uses UTF-8 encoding to identify Unicode text.
The representation of a string is simple, and is created using double quotation marks ("") or anti-quotation marks (' '). For example: "Hello World" or "Hello World".

The difference between the two:

The escape character between the double quotes is escaped, and the characters between the anti-quotes remain unchanged.

//示例代码    var frenchHello string  // 声明变量为字符串的一般方法    var emptyString string = ""  // 声明了一个字符串变量,初始化为空字符串    func test() {        no, yes, maybe := "no", "yes", "maybe"  // 简短声明,同时声明多个变量        japaneseHello := "Konichiwa"  // 同上        frenchHello = "Bonjour"  // 常规赋值    }
s := "hello"c := []byte(s)  // 将字符串 s 转换为 []byte 类型c[0] = 'c's2 := string(c)  // 再转换回 string 类型fmt.Printf("%s\n", s2)

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.