Reading Golang Web Programming

Source: Internet
Author: User
Tags delete key

Installation tool for 1.golang

1.1 GVM A Go multi-version management tool developed by third parties

2.golang Environment variables

2.1 Goroot=d:\go (Golang installation directory)

2.2 Gobin=d:\go\bin (Golang generated bin directory)

2.3 Gopat=d:\code\golang (Golang workspace, allows multiple directories, separated)

When you execute the goget command, the package is placed in the first configured Gopath directory

Gopath Convention Three subdirectories: src (source code), bin (compiled file,. a), pkg (compiled executable file)

3. Go command

3.1 Go build compiles the executable file in the current directory.

3.2 Go install the build executable file in the bin directory.

3.3 Go get Download remote package, the-u parameter automatically updates the package, and all dependent packages are downloaded

Example: Go get github.com/astaxie/beedb

3.4 Go FMT format code

3.5 Go test automatically reads a file named *_test.go in the directory, generates and runs the test executable file

4. Go Grammar Basics

4.1 Defining variables

4.1.1 Var name type | Define a variable

4.1.2 var name1,name2 type | Define multiple variables

4.1.3 var name1 type = value | Defining variables and initializing values

4.1.4 var n1,n2,n3 type = v1, v2, v3 | Simultaneous initialization of multiple variables

4.1.5 var n1,n2,n3 = v1, v2, v3 | Short statement first edition, suitable for global variables

4.1.6 n1,n2,n3: = V1,v2,v3 | Short declaration of the final version, only suitable for internal functions, external cannot be compiled through

4.1.7 _,b:=34,35 | The underscore is a special variable name, and any value given to it will be discarded

4.2 Constants

4.2.1 Const NAME = value | Define a constant

4.2.2 Const Pi float32 = 3.1415926 | Defines a constant and specifies the type.

4.3 Built-in base type

4.3.1 bool type with a value of true or False

4.3.2 int and unit types, also some specified types: Rune, int8, Int16, Int64 and Byte, Unit8, UInt16, Uint32,uint64, where Rune is the nickname for Int32, and Byte is the nickname for Unit8. These types of variables are not allowed to be assigned to each other, and errors are encountered at compile time.

4.3.3 string string type, defined with a pair of "" or "

For example S1, s2, s3: = "A", "B", "C"

String values can not be directly modified, such as "Hello", Want to modify "H" for "C", there are two ways: 1, first turn to byte array after change back to string 2, using slice s2: = "D" + s1[1:]

4.3.4 error type, specifically for handling errors

4.3.4 Iota Enumeration, default is starting from 0

Const (
deleted = Iota
Checked
Invalid = 3
)

4.3.4 Go Programming Rules

Variables that begin with capital letters are exportable and are common variables

A variable that starts with a lowercase letter is non-exportable and is a private variable

A function that starts with a capital letter, is a common function

A function that starts with a lowercase letter, is a private function

4.3.5 Array

var arr [N]type | Define an array

arr:=[3]int{1,2,3} | Brief statement

arr:=[10]int{1,2,3} | Short statement, other defaults to 0

Arr:=[...] int{4,5,6} | Automatically calculates length based on the number of elements

doublearr:=[2][4]int{[4]int{1,2,3,4},[4]int{5,6,7,8}} | Declaring a two-dimensional array

doublearr:=[2][4]int{{1,2,3,4},{5,6,7,8}} | If the inner element is the same as the outer one, simplify the wording

An array is an immutable length.

The assignment between arrays is the assignment of a value, that is, when an array is passed as a parameter to the function transports, it is actually a copy of the array, not its pointer.

4.3.6 slice "Dynamic array": Not a dynamic array in real sense, but a reference type

var Fslice []int | Define a slice

Slice:=[]byte{' A ', ' B ', ' C '} | Define and initialize

b = Ar[5:10] | Take 5-10 fragments of data

4.3.7 Map Dictionary

var numbers map[string]int | Define a dictionary

Numbers: = Make (Map[string]int) |

Rating: = map[string]float32{"C": 5, "Go": 4.5, "Python": 4.5, "C + +": 2} | Initialization

Delete (Map,key) | Delete key

Maps are unordered, and each time you print out a map, it will be different.

The length of the map is not fixed, as is the case with slice, which is a reference type

4.3.8 make memory allocations for built-in types (map, slice, channel)

NEW: Initializes the object, returning the first pointer to the object. Can be used to initialize any type

Make: Returns an initialized instance, not a pointer.

4.4 If Else

A variable is allowed to be declared in a conditional judgment statement, which acts on the logical block of the condition and does not work elsewhere.

if x: = 1; X > 0 {
Fmt. Print ("X is true")
}

4.5 goto

Used to jump to a label defined within the current function

Func Main () {
I: = 0
Here:
println (i)
i++
If I < 10 {
Goto HERE
}
Fmt. Println ("Finish")
}

4.6 for

For exp1;exp2;exp3{

}

EXP1 and EXP3 are variable declarations or function call return values. EXP2 is used to determine the condition.

Func Main () {
Sum: = 0
For index: = 0; Index < 10; index++ {
Sum + = Index
}
Fmt. Println ("Sum is equal to", sum)
}

Func Main () {
MP: = Make (Map[string]int, 10)
Mp["a"] = 1
Mp["B"] = 2
For k, V: = Range MP {
Fmt. PRINTLN ("Map ' s key:", K)
Fmt. PRINTLN ("Map ' s value:", V)
}
}

4.7 Switch

Fallthrough can enforce the next case

5. Functions

5.1 Defining functions

Func funcName (input1 type1, Input2 type2) (output1 type1, Output2 type2) {
Here is the process logic code
Return multiple values
return value1, value2
}

5.2 Defining a variable parameter function

Func myfunc (arg ... int) {}

5.3 Pass-through value and pass-through pointer

The pointer uses multiple functions to manipulate the same object

The pointer is relatively lightweight (8bytes), just pass the memory address, when the large structure of the time, you can save (memory and time)

5.4 Defer

Deferred statement, which executes on the last side of the function

Remember: Defer is advanced after out, when a function defines multiple defer, the last one executes first.

5.5 functions as values, types

Type typeName func (input1 inputType1 [, Input2 inputType2 [, ...]) (Result1 ResultType1
[, ...])

5.6 Panic and recover

Func Main () {
Fmt. Println ("Hello")
Defer func () {
If err: = Recover (); Err! = Nil {
Fmt. PRINTLN (ERR)
}
}()
Panic ("I am Error")
}

5.6 Main and INIT functions

6. Import

Package import feature that supports two modes of loading

6.1 Relative path Import

Import "./model" | The model directory of the same directory as the current file is not recommended for use

6.2 Absolute Path Import

Import "Shorturl/mode"

6.3. Operators

Import (. " FMT ") | When calling a method, the package name can be ignored, for example: Println ("Hello")

6.4 Alias Operation

Import (F "FMT") | When used: f.println ("Hello")

6.5 _ Operation

Import (_ "Github.com/ziutek/xxx")

The operation is actually to introduce the package, instead of directly using the function in the package, instead of calling the Init function

7. struct Structure body

7.1 Defining the structure body

Type person struct {
Name string
Age int
}

7.2 Initialization

var person person | Statement

Person: = Person{name: "Jxp", age:12} | declaring and assigning values

7.3 Structure method

Func (c *person) say (what string) {
Fmt. Println (what)
}

7.4 Structure in vivo poly

Type Human struct {
Name string
Age int
}

Person ...
Type person struct {
Human
Sex string
Hobbit string
}

Func (c person) say (what string) {
Fmt. Println (what)
}

Func Main () {
P: = person{human:human{name: "Jxp", Age:12}, Sex: "Man", Hobbit: "AA"}
Fmt. PRINTLN (P)
}

Reading Golang Web Programming

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.