The type of Go language learning

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

Preface : will become a Golang language developers, the process of learning their own record and share with you, hope to bring you help, if in the process of writing something wrong or I say the wrong place also please enlighten us, Give me a message I'm good in time to make changes to prevent misleading other readers, thank you in advance!
statement: The contents of this question are from "Go language Programming", "Go language learning note", "Go concurrent programming Combat", and video "Follow the No-go" video connection as follows: http://edu.51cto.com/course/course_id-1762.html

The type of Go Language Foundation

Go语言的关键字不多,下面我们就列举下Go语言的基本数据类型
type length Default Value Description
bool 1 False Boolean type, true false for False
Byte 1 0 BYTE type
Int,uint 4,8 0 Default integer type, based on target platform 32 or 64 bits
Int8,uint8 1 0 A 8-bit binary number represents a signed integer type with unsigned
Int16,uint16 2 0 A 16-bit binary number represents a signed integer type with unsigned
Int32,uint32 4 0 A 32-bit binary number represents a signed integer type with unsigned
Int64,uint64 8 0 A 64-bit binary number represents a signed integer type with unsigned
Float32 4 0.0 Floating-point types
Float64 8 0.0 Default floating-point type
Complex64 8- -
complex128 16 - -
Rune 4 0 Go language specific types are designed to store Unicode encoding
UIntPtr 4,8 0 Pointer
String - “” String, default is empty string NOT NULL
Array - - Array
struct Structural body
function Nil Function
Interface Nil Interface
Map Nil Dictionary, reference type
Slice Nil Slices, reference types
Channel Nil Channel, reference type

Definition of a variable

Defined

The keyword VAR is used for the definition of variables, in other languages The Go language defines variables that place the type behind the variable name.

varint           //定义一个int类型的变量,自动初始化为0varfalse       //自动推断为bool类型varint       //一次定义多个变量var100,"abc"//不同类型的初始化//也可以以组的方式定义多个变量:var(        int        100,"abc")

Short mode

In addition to using the keyword ' var ', you can define and initialize variables in a much shorter way:
x: = 10
A,s: = 1, "abc"
Note: Use the short mode to be aware of the following issues:
1 defining simultaneous display initialization of variables
2 cannot provide data type
3 can only be used inside a function

Multi-variable assignment

Believe that a lot of C + + programmers have encountered the exchange of two variables worth the problem, in C + + need to introduce a third variable to complete the worth of exchange, in the Go language provides a multi-variable assignment method to omit the third variable.

    x,y := 1,2    x,y = y+3,x+2    //此时x的值为5,y的值为3

Named

With all that said, we should also talk about commands in the go language, naming variables, constants, functions, custom types, and usually giving precedence to letters or combinations that have practical meaning and are easy to read. Note that there is no class in the go language and no public keyword in the go language is a method or variable that is externally accessible by capitalizing the first letter.

Naming recommendations:

    • Start with a letter or a glide line, composed of multiple letters, numbers, and underscores
    • Case sensitive
    • Use hump spelling format
    • Local variables take precedence over short names
    • Don't use reserved keywords
    • Do not use the same name as the constant, type, built-in function
    • Proprietary names are usually all uppercase, such as escapehtml

Null identifier

There is a special member called "_" in the Go language, which is usually used as a placeholder for the omission, and can be left-valued as an expression.
X,_,z: =
The above example is not obvious, but is handy when used with multiple return values of a function.

Constant

It is believed that C + + programmers are not strangers to constants, constants represent a constant immutable value, and the ' const ' modifier is also used in the go language.

constint1,2const"hello"const(    1,3.14    false)

Enumeration

The go language does not have an explicit enum definition, but you can implement an enumeration type by using the ' iota ' identifier to implement a set of self-increment constant values.

const(    iota    //x值为0    y           // 1    z           // 2)

The above can be seen that iota is self-increasing if interrupts must show recovery

const(    iota   // a为0    b          // 1    c = 100    // c的值为100    d          // d的值为100    iota   // e得值为4此时恢复了自增    f          // 5 )

The self-increment default type is int also can be displayed for the type of enactment

const(    iota           //int    float32iota   //float32    intiota       //int)

In practical use, it is recommended that custom types be used to implement well-defined enumeration types.
Type color byte//custom type
Cont
Black color = iota//Set Constant type
Red
Blue
)

Reference type

Reference types specifically refer to the three predefined types of slice, map, channel, and more complex storage structures compared to type reference types such as numbers, arrays, and so on. In addition to allocating memory, they also need to initialize a series of properties, such as pointers, lengths, even hash distributions, data queues, and so on.
The reference type will be described in more detail later in the chapter.

Custom Types

Custom types Use the keyword ' type ', and VAR is similar to const, where multiple type definitions can be combined into groups to define a local type within a function or code block.

type  data  int  var  D data  = ten   
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.