Go Language Learning-Basic (2)

Source: Internet
Author: User
Tags arithmetic control characters

One, go data types and operators

1, Boolean type

The bool type has only two values in the Go language: true and false; unlike other languages, 0 and 1 can also represent Boolean types, which are not allowed in the go language, and define a bool type with a default value of False

A. var b bool (default = False)

B. var b bool = True

C. var B = True

2, operator

A. = = equals

B. = Does not equal

C.!B Reverse operation

D. && and operations

E. | | or arithmetic

Boolean-type formatted output placeholder%t

3, integer and floating-point type

A. Int8,int16,int32,int64 (signed integer type) where numbers indicate how many bits

B. Uint8,uint16,uint32,unit64 (unsigned integer type)

c. int and uint, related to the operating system platform

D. float32 and float64 floating-point numbers

Note: The go language is a strongly typed language, in fact, the int type, can not directly convert between bytes, for example: int8 type of variable, can not be directly assigned to the value Int16

All integers are initialized to 0, all floating-point numbers are initialized to 0.0, and Boolean types are initialized to False

4, plural type

A, two forms: Complex64 and complex128

B,var C complex64 = 5 +10i

C, built-in function complex (RE,IM) constructs a complex number

D, universal output placeholder:%v

5, bit operation

A, with Operation:&

1 & 1 = 1;1 & 0 =0; 0 & 1 = 0;0 & 0 =0

b, or operation

1 | 1 = 1;1 | 0 =1;0 | 1 =1;0 & 0 =0

C, XOR or operation

1 ^1 = 0;1 | =1;0 | = 1; 0 & 0 =1

6, bitwise operator

A. Shift left << 1<< 10 = 1024

B. Shift right >> 1024x768 >> 10 = 1

7, operator

A. Logical operator, ==,! =,<,<=,>=

B. Arithmetic operator, +,-,*,,,%

Two, go language code specification

1, in the Go language any one code file belongs to a package

2,import keywords, referencing other packages:

Import ("FMT")

Importing multiple Packages
Import (

"FMT"

"OS"

)

3, the same package function call, directly call, if you import other packages, you need to add the package name + dot + function name, such as Calc.add,calc for registration, add for one of the functions

4, Package access control rules:

A. The function name or the beginning of the variable is capitalized, which means that the function or variable can be imported into other source code.

B. function name or variable at the beginning of the character lowercase, meaning that the function or variable, can not be exported, as a private property

5, each source file can contain an init function, the init function is automatically called by the Go Run framework, example

Package Mainimport (     "FMT") func init () {                                  
Fmt. Println ("Init called")}func Main () { fmt. Println ("main called") #先输出init called, final output main called}

6. Only initialize the package, do not reference, in the Go language, in general, a package does not reference, is not to import, otherwise it will compile an error, but there is also a situation can be imported package, initialization, that is, before the package name plus _

Import (

_ "Add"

Three, constant

1. Constants are modified using const, and are always read-only and cannot be modified

2. Const can only modify boolean,number (int correlation type, floating point number, complex) and string

3. const definition const identifier [type] = value

4. Multiple values define a const (

A = 1

b = 2

c = 3

)

5. Application of Iota Word const Iota Initial value is 0, define parameters later, if additional values are appended, sequentially plus 1, the value of the new const iota is reset

Package Mainimport (  "FMT") const (A = IOTABC) const (d = iotae) Func main () {FMT. Printf ("%d,%d,%d,%d,%d", A,b,c,d,e)} output is: 0,1,2,0,1

6, in const assignment, if a variable is not assigned, then his value defaults to the value of the previous expression

Iv. built-in packages in the Go language

1.time

Func Main () {now      : = time. Now ()                              #获取当前时间      Second: =now. Unix ()                            #将当前时间转化成时间戳}

2, OS

A. Get host name

Func Main () {    Name,ret: = os. Hostname ()    FMT. Printf ("%s%v", Name.err)}

B. Get Gopath

Func Main () {        val: = os. The Getenv ("PATH")             #os. GETENV plus variable parameter        FMT. Printf ("%s\n", Val)}

Five, string

1, String Declaration

A. var b =string

b. var b = "Hello"

C. var b string = "Hello"

2. Quotes in the Go language

A. Double quotes, "", can contain control characters

B. Anti-quotes, • •, anti-quote characters are output as-is

3. String manipulation

A. Length: Len (str)

Example

Func Stroperator () {      str: = ' Hello World ' for      index,val: = Range str {         FMT. Printf ("len:%d\n", Len (str))       }}

B. Splicing: +,fmt. Sprintf

STR2: =str + "World"

C. Split: String. Split, returns an array

str= "Hello World" result: = Strings. Split (str, "")         #已空格分割fmt. Printf ("Result"%v\n ", result)         #返回结果为数组

D. Contains: String. Contains

str= "Hello World" result: = Strings. Contains (str, "he")         FMT. Printf ("Result"%v\n ", result)         #返回true或者false

E. prefix or suffix judgment: strings. Hasprefix,strings. Hassuffix

str = "Baidu.com" if ret: = Strings. Hasprefix (STR4, "http://"); RET = = False {        STR4 = "/http" + str  }

F. Where the substring appears: string. Index (), strings. Lastlndex ()

G. Join operation: String. Join (A[]string,sep string)

Six, flow control statements

1. If statement

If conditionl {

Do something

} else if Condition2 {

Do something

} else {

}

2. If syntax sugar

If b:=100;b> 200 {

Do something

}

3. For loop

A initialization condition modification

For init;condition;modify {}

b Infinite Loop

for {}

For true {}

C for traversing strings, arrays, slices

For index,val: =range str {

}

Go Language Learning-Basic (2)

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.