"Go" Go language learning note one

Source: Internet
Author: User
Tags constant definition

A, why learn the go language?

From a personal point of view, the first is by the go language legend of the ultra-high development efficiency and efficiency of the attraction; the second is that the go language supports concurrency at the language level, which is very convenient in today's programming business; The third is because the first two points, I think in the future go will fire up; Follow Microsoft or Google will always have meat to eat ^_^!

B, the first GO program

According to international practice, learn a language, always start with a Hello world, again, see the first go program to feel the Go

Package Mainimport "FMT" Func Main () {    fmt. Println ("Hello world!")}

Package---Packet declaration that indicates which package this code belongs to

Import---Importing a package, importing FMT because you need to use the println function in the FMT package//cannot import packages that are not used in the code, compile will error

/****************** Start Language *********************/

First, sequential programming

1. Variables

A, statement:

Introducing the keyword VAR, type information after the variable name

var a int
var v2 string
var v3 [ten]int //array
/* Multiple variables declared together */
VAR (
v1 int
V2 string
)

B, initialization

var v1 int = 10var v2 = 10//can automatically deduce type v3: =//v3 cannot be a variable that has already been declared

C, assigned value

var v4 INTV4 = 10
/* Multiple assignments */
A, B = 2, 3
I, j = j, I

D, anonymous variables

The go language supports multiple return values and anonymous variables, because functions can return multiple values, and can be selectively received when received

Func getName (FirstName, LastName, nickname) {    return "Zhangsan", "Lisi", "Wuhaha"}_, _, Nickname: = GetName ()

2. Constants

A, literal constants-----hard-coded constants in the program (no type)

-12 3.14159265358979325846//floating-point constant 3.2 + 12i//plural constant true//Boolean constant "foo"//String type constant

b, constant definition

Keyword const

Const Pi float64 = 3.14159265358979323846const (Size int64 = 1024x768 EOF =-1) Const MASK = 1 << 3//constant expression for compile-time operation

Constant assignment is a compile-time behavior, so the right value cannot be any expression that requires a run-time to produce a result

The following errors are chestnuts:

Const Errooooor = os. GETENV ("HOME")//os. GETENV () to produce results at run time

C, pre-defined constants

True, False, iota

Iota---Constants that can be modified by the compiler, appear once per const, iota reset to 0, and do not appear the number represented by the Iota,iota once before the next const appears +1

Const (    C0 = Iota//co = = 0    C1 = Iota//c1 = = 1    C2 = Iota//c2 = = 2     ) const (    C0 = 1 << Iota//c0 = = 1    c1 = 1 << Iota//c1 = 2    c2 = 1 << Iota//c2 = = 4                 )

D, enumeration---Define a set of constants

Const (
Sunday = Iota
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Numberofdays//This constant is not exported
)

As with other symbols in the go language, constants that begin with a capital letter are visible outside the package . In the example above , Numberofdays is private in the package and other symbols can be accessed by other packages .

  

  

  

  

  

"Go" Go language learning note one

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.