Getting Started with Go language basics-Variables

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

1. Go Variable declaration

The Go language basics comes from "Go language Programming", personal records, and deeper understanding.

the variable declaration of the Go language differs from the C + + language. The go language introduces the var keyword. The variable declarations are as follows:
var v1 int//declares an integer variable V1var v2 stringvar v3 [10]int//array var v4 []int//Array tile var v5 struct {//struct  F int}var V6 *int//pointer var V7 map[string] Int//map,ley to String,value for Intvar V8 func (a int) int
as you can see, the declaration of a variable has no semicolon; as a terminator the var keyword can also define multiple variables at once
<pre name= "code" class= "CPP" >var (
  v1 int
  V2 string
)

2. Variable initialization

The var keyword is defined in a variable to be omitted, such as:
var v1 int = 10//correct usage 1var v2 = 10//correct use mode 2, the compiler can automatically deduce the type of v2 v3: = 10//correct use of 3, the compiler can automatically deduce the type of V3
use: = can reduce the amount of code written, which is a great benefit to programmers but in the course of our use, it should not appear as follows:
var i inti: = 2
This causes a compilation error, such as an error: no new variables on left side of: =

3. Assigning values to variables

<pre name= "code" class= "CPP" style= "Font-family:courier;" >var V10 int//First declare, then assign value v10 = 123
I, j = 2, 3//go supports multiple assignments
The simplest benefit of multiple assignments is the exchange of two-digit values. Such as:
I, j = j, I

The compiler calculates the value to the right of the equal sign, and then assigns the value to the left

4, the amount of anonymity

The Go language supports multiple return values and anonymous variables
Func GetName (FirstName, LastName, nickname string) {return "may", "Chan", "Chibi Maruko"}
The function returns 3 variables of type string
FirstName, LastName, nickname
We can return the anonymous variable directly without a new definition of the variable. This makes our code more concise and elegant. Of course, we can also choose to receive parameters when the receiving function returns.
_, _, Nickname: = GetName ()

5. Constants

in the Go language, constants refer to values that are known and immutable during compilation. Constants can be numeric types (including integer, float, and complex types), Boolean types, string types, and so on.
literal Constants
-123.14159265358979323846//constant of floating-point type 3.2+12i//constant of plural type true//Boolean type constant "foo"//String constant
constant Definition
Define constants with the Const keyword:
Const Pi float64 = 3.14159265358979323846const zero = 0.0//non-type floating-point constant const (size int64 = 1024x768 EOF =-1//untyped integer constant) const U, V float32 = 0, 3//U = 0.0, v = 3.0, constant multi-assignment const A, b, C = 3, 4, "foo"//a = 3, B = 4, c = "foo", untyped integer and string constant
If a constant is defined without a qualified type, then the constant is a literal constant the right-hand value of a constant definition can also be a constant expression that is evaluated at compile time, such as:
Const MASK = 1 << 3
because the assignment of a constant is a compile-time behavior, the right value cannot have any expression that requires a run-time to produce a result.
, such as attempting to define a constant in the following way causes a compilation error:
Const HOME = os. GETENV ("HOME")
The reason is simple, os. GETENV () The return result is known only at run time and is not determined at compile time, so
The right value cannot be defined as a constant. pre-defined constants The Go language pre-defines these constants: True, False, and iota.
Iota is special and can be thought of as a constant that can be modified by the compiler, and when each const keyword appears, it is
Resets to 0, and then each time a iota appears before the next const appears, the number it represents is automatically increased by 1.
The following example can be used to understand the usage of iota basically:
Const (  //iota reset to 0C0 = iota//C0 = = 0C1 = Iota//C1 = 1C2 = Iota//c2 = = 2) const (A = 1 << iota//A = = 1 (Iota at each const start is reset to 0) b = 1 << iota//b = = 2c = 1 << iota//c = = 4) const (U = Iota *//U = 0v float64 = Iota *//V = = 42.0
W = Iota *//W = = +) Const X = iota//x = = 0 (because iota is reset to 0) const Y = iota//y = = 0 (ibid.)
If the expression of the two Const assignment statement is the same, then the latter assignment expression can be omitted. Therefore, on
The first two const statements of a polygon can be abbreviated as:
Const (///iota is reset to 0c0 = iota//C0 = 0C1//C1 = 1C2//c2 = 2) const (A = 1 <<iota//A = = 1 (iota is reset at each const start) is 0) b/b = = 2c//c = = 4)

6. Enumeration

enumeration refers to a series of related constants, such as the following about the daily definition of one weeks. Using the example in the previous section, we
See that you can define a set of constants in the form of a const followed by a pair of parentheses, which is commonly used in the go language to define
The enumeration value. The go language does not support enum keywords that are explicitly supported in many other languages.
Const (Sunday = iotamondaytuesdaywednesdaythursdayfridaysaturdaynumberofdays//This constant is not exported)
As with other symbols in the go language, constants beginning with uppercase letters are visible outside the package.
In the example above, Numberofdays is private in the package and other symbols can be accessed by other packages.


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.