Go language basics

Source: Internet
Author: User
Tags uppercase letter
Go language basics

This section describes how to define variables, constants, built-in types of Go language, and some skills in go language programming.

Define Variables

There are multiple ways to define variables in the go language.

Using the VaR keyword is the most basic way to define variables in the go language. Unlike the C language, the Go language places the variable type behind the variable name, as shown below.

// Define a variable named "variablename" and of the type "type"

VaR variablename type

-Multiple variables are defined.

// Define three variables whose types are "type"

VaR vname1, vname2, vname3 type

-Define variables and initialize values.

// The variable for initializing "variablename" is the value of "value" and the type is "type"

VaR variablename type = Value

-Multiple variables are initialized at the same time.

/*

Define three types, which are all three variables of "type" and initialize the corresponding values respectively.

Vname1 is V1, vname2 is V2, and vname3 is v3

*/

VaR vname1, vname2, vname3 type = V1, V2, V3

-Do you think the above definition is cumbersome? It doesn't matter, because the designers of the Go language have also discovered that there is a way to simplify it. We can directly ignore the type declaration, so the above Code becomes as follows.

/*

Define three variables and initialize corresponding values respectively.

Vname1 is V1, vname2 is V2, and vname3 is v3

Then go will help you initialize them based on their corresponding value types.

*/

VaR vname1, vname2, vname3 = V1, V2, V3

-Do you think the above is complicated? Okay, let's continue to simplify it.

/*

Define three variables and initialize corresponding values respectively.

Vname1 is V1, vname2 is V2, and vname3 is v3

The compiler automatically exports the corresponding type based on the initialization value.

*/

Vname1, vname2, vname3: = V1, V2, V3

Does it seem very concise now? : = "Directly replaces VaR and type. This form is called a short statement. However, it has a limitation that it can only be used inside a function. If it is used outside a function, it cannot be compiled and passed. Therefore, global variables are generally defined using the VaR method.

_ (Offline) is a special variable name, and any value assigned to it will be discarded. In this example, we assign the value 35 to B and discard 34 at the same time.

_, B: = 34, 35

The Go language reports an error during compilation for declared but unused variables. For example, the following code produces an error: I is declared but not used.

Package main

 

Func main (){

VaR I int

}

Constant

The so-called constant, that is, the value determined during the program compilation phase, and the value cannot be changed during the program runtime. In a go program, constants can be defined as numerical values, Boolean values, or strings.

Its syntax is as follows.

Const constantname = Value

// You can specify the constant type if needed:

Const PI float32 = 3.1415926

-The following are examples of constant declarations.

Const Pi = 3.1415926

Const I = 10000

Const maxthread = 10

Const prefix = "astaxie _"

Built-in basic type boolean

In the go language, the Boolean value is of the bool type and the value is true or false. The default value is false.

// Sample code

VaR isactivebool // global variable Declaration

VaR enabled, Disabled = true, false // ignore type declaration

Func test (){

VaR available bool // General Declaration

Valid: = false // short statement

Available = true // value assignment operation

}

Value Type

There are two types of integers: signed and signed. The Go language supports both int and uint types, which have the same length, but the specific length depends on the implementation of different compilers. The current GCC and gccgo compilers use 32-bit to represent int and uint on both 32-bit and 64-bit platforms, but may increase to 64-bit in the future. The Go language also has the following types of digits: Rune, int8, int16, int32, int64, byte, uint8, uint16, uint32, and uint64. Rune is the alias of int32 and byte is the alias of uint8.

Note that values or operations cannot be assigned to each other between these types of variables. Otherwise, the compiler reports an error during compilation.

For example, the following code produces an error.

VaR A int8

VaR B int32

C: = a + B

In addition, although the length of Int Is 32 bit, int and int32 cannot be used together.

Float32 and float64 (no float type) are supported. The default value is float64.

Is that all? The Go language also supports the plural. Its default type is complex128 (64-bit real number + 64-bit virtual number ). If you need a smaller value, there will also be complex64 (32-bit real number + 32-bit virtual number ). The form of the plural is re + IMI, where Re is the real part, Im is the virtual part, and the final I is the virtual unit. The following is an example using the plural number.

VaR C complex64 = 5 + 5I

// Output: (5 + 5I)

FMT. printf ("value is: % v", c)

String

As we mentioned in the previous section, strings in the go language are all encoded using the UTF-8 character set. A string is defined by a pair of double quotation marks ("") or reverse quotation marks (''). Its type is string.

// Sample code

VaR frenchhello string // a general method for declaring a variable as a string

VaR emptystring string = "" // declares a string variable, which is initialized as a Null String

Func test (){

No, yes, maybe: = "no", "yes", "maybe" // a brief statement that declares multiple variables at the same time

Japanesehello: = "ohaiou" // same as above

Frenchhello = "bonjour" // regular value assignment

}

-Strings in the go language are unchangeable. For example, an error is reported during compilation of the following code.

VaR s string = "hello"

S [0] = 'C'

-But what if I really want to modify it? The following code can be implemented.

S: = "hello"

C: = [] Byte (s) // convert string s to [] Byte type

C [0] = 'C'

S2: = string (c) // returns the string type.

FMT. printf ("% s \ n", S2)

-The "+" operator can be used in the go language to connect two strings.

S: = "hello ,"

M: = "world"

A: = S + m

FMT. printf ("% s \ n",)

-The modified string can also be written

S: = "hello"

S = "C" + s [1:] // although the string cannot be changed, the slice operation can be performed.

FMT. printf ("% s \ n", S)

-What if I want to declare a multi-line string? You can declare it through.

M: = 'Hello

World'

The string enclosed by "'" is a raw string, that is, the form of the string in the Code is the form of printing, it does not have character escape, line feed will also be output as is.

Error Type

The Go language has an internal error type, which is used to handle error information. The package in the go language also has a dedicated package errors to handle errors.

Err: = errors. New ("emit macho dwarf: Elf header already upted ")

If Err! = Nil {

FMT. Print (ERR)

}

Underlying storage of Go language data

Figure 2.1 From russcox blog (http://research.swtch.com/godata) an article about the Go language data structure, we can see that these basic types of underlying are allocated a piece of memory, and then stored the corresponding value.


Figure 2.1 storage of the Go language data format

TIPS: grouping statement

When multiple constants and variables are declared at the same time in the go language, or when multiple packages are imported, declarations can be made in groups.

For example, the following code.

Import "FMT"

Import "OS"

 

Const I = 100

Const Pi = 3.1415

Const prefix = "Go _"

 

VaR I int

VaR PI float32

VaR prefix string

-Groups can be written as follows.

Import (

"FMT"

"OS"

)

 

Const (

I = 100.

Pi = 1, 3.1415

Prefix = "Go _"

)

 

VaR (

I int

Pi float32

Prefix string

)

Unless explicitly set to another value or iota, the first constant of each const group is set to its 0 value by default, the second and subsequent constants are set to the value of the previous constant by default. If the value of the previous constant is iota, it is also set to iota.

Iota Enumeration

The Go language contains the keyword iota, which is used to declare enum. The default start value is 0, and 1 is added for each call.

Const (

X = iota // X = 0

Y = iota // y = 1

Z = iota // z = 2

W // when the null value is declared as a constant, the default value is the same as the literal value of the previous one. Here, W = iota is implicitly stated, So w = 3. In fact, the above Y and Z can also not use "= iota"

)

 

Const v = iota // every time a const keyword is encountered, iota is reset. At this time, V = 0

Rules for programming in the go Language

The Go language is concise because it has some default behaviors.

-Variables starting with an upper-case letter can be exported, that is, other packages can be read and are public variables. Variables starting with a lower-case letter cannot be exported and are private variables.

-The same is true for functions starting with an uppercase letter, which is equivalent to a public function with a public keyword in the class. A lowercase letter starts with a private keyword.

 

This article is excerpted from go web programming.

Xie mengjun

Published by Electronic Industry Publishing House

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.