This is a creation in Article, where the information may have evolved or changed.
Introducing Go
Directory
- 1 Get Started
- 2 type
- 3 variables
- 4 Control Structure
- 5 Arrays, Slices, and Maps
- 6 Functions
- 7 Structs and Interfaces
- 8 Package
- 9 Testing
- Ten Concurrency
- One Next Steps
Get Started
Type
Variable
Control structure
- The for i<=10 {...}//for keyword is used here as a while
- If: Why not add () here? With the SWIFT study? Or with Python?
Arrays, Slices, and Maps
- x: = Make (Map[string]int)//map The default value is nil, all must first make, in order to go inside the add element
- Nested map (JSON? ): Elements: = map[string]map[string]string {...
Functions
- Variable parameter://Is this the run-time or the compiler?
- Func Println (A ... interface{}) (n int, err error)
- xs: = []int{1,2,3}; Fmt. Println (Add (xs ...))
- Closed Package
- A closure is supported if the external reference variable is allocated on the heap, but go can return directly to the function and the C language can only return function pointers what is the difference?
- Defer, panic, recover
- * and &
Structs and Interfaces
- c: = &circle{0, 0, 5}//This is probably the most normal use of a struct in the go language?
- Because the parameters are all value-passed (? ), so the function generally uses pointers to pass parameters? C language is not the same value, C + + can be quoted &
- Func (R *rectangle) area () float64 {...}
- ==> r: = rectangle{0, 0, 10, 10}; R.area ()
- Expression is-a: Embedded type (as if the other language is also this anonymous style?) )
- Interfaces
- type Shape interface {area () Float64}
Package
- Strings
- Strings. Contains ("Test", "es")//Why not extend the method directly on the string object?
- Strings. Join ([]string{"A", "B"}, "-")
- Strings. Replace ("AAAA", "a", "B", 2)///The last parameter represents the maximum number of replacements? Why not startindex?
- Io:reader and writer
- Func Copy (DST Writer, SRC Reader) (written int64, err error)
- bytes. Buffer
- Files and Folders
- File, err: = OS. Open ("Test.txt")//rw?
- Stat, err: = file. Stat () ==> Stat. Size ()
- Os. Open ("."). Readdir (-1)//This code is a little bit too low.
- Directory traversal using the package Path/filepath :
- FilePath. Walk (".", func (path string, info OS.) FileInfo, err Error) error {... (Return to FilePath.) Skipdir stop Traversal)}//Does not seem to have control options ah?
- container/list://double linked list?? Is there a higher level of data structure
- Sort. Sort
- Requires (array) object implementation 3 methods: Len () int, less (i, J int) bool, swap (i, J int)//go language supports A,b=b,a Exchange assignment syntax
- Hash and cryptography *
- Servers
- Tcp
- Listener, err: = Net. Listen ("TCP", ": 9999")
- For {con, err: = Listener. Accept ()//hmm? Must you receive a connection to receive the next one?
- ERR: = Gob. Newdecoder (c). Decode (&msg)//var msg string; What is the codec algorithm for GOB?
- Client:
- C, Err: = Net. Dial ("TCP", "127.0.0.1:9999")
- HTTP
- http. Handlefunc ("/hello", hello)//func Hello (res http. Responsewriter, req *http. Request) {...}
- http. Listenandserve (": 9000", nil)
- Static file: http. Handle ("/assets/", http. Stripprefix ("/assets/", http. Fileserver (http. Dir ("assets")),)//Here is not a few more commas?
- RPC (slightly)
- Parsing command-line arguments
- Flag. Parse ()
Testing
- _test.go
- Import "Testing"
- Func testaverage (t *testing. T) {... t.error (...) ... }
- $ go Test
Concurrent
- < create channel and pass it to goroutine>
- Select {
- Case <-time. After (time. Second)://Timeout
- c: = make (chan int, 1)//buffered channel is asynchronous (but exceeding the limit will still block it)
- Initiate an HTTP GET request: res, err: = http. Get (URL)//Is this too concise? (No settings requested for header)
Next Steps
- Read the code implementation under go pkg?