Introducing Go-o ' Reilly 2016 reading notes

Source: Internet
Author: User
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

    1. The for i<=10 {...}//for keyword is used here as a while
    2. If: Why not add () here? With the SWIFT study? Or with Python?

Arrays, Slices, and Maps

    1. x: = Make (Map[string]int)//map The default value is nil, all must first make, in order to go inside the add element
    2. Nested map (JSON? ): Elements: = map[string]map[string]string {...

Functions

    1. Variable parameter://Is this the run-time or the compiler?
      1. Func Println (A ... interface{}) (n int, err error)
      2. xs: = []int{1,2,3}; Fmt. Println (Add (xs ...))
    2. Closed Package
      1. 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?
    3. Defer, panic, recover
    4. * and &

Structs and Interfaces

    1. c: = &circle{0, 0, 5}//This is probably the most normal use of a struct in the go language?
    2. 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 &
    3. Func (R *rectangle) area () float64 {...}
      1. ==> r: = rectangle{0, 0, 10, 10}; R.area ()
    4. Expression is-a: Embedded type (as if the other language is also this anonymous style?) )
    5. Interfaces
      1. type Shape interface {area () Float64}

Package

  1. Strings
    1. Strings. Contains ("Test", "es")//Why not extend the method directly on the string object?
    2. Strings. Join ([]string{"A", "B"}, "-")
    3. Strings. Replace ("AAAA", "a", "B", 2)///The last parameter represents the maximum number of replacements? Why not startindex?
  2. Io:reader and writer
    1. Func Copy (DST Writer, SRC Reader) (written int64, err error)
    2. bytes. Buffer
  3. Files and Folders
    1. File, err: = OS. Open ("Test.txt")//rw?
    2. Stat, err: = file. Stat () ==> Stat. Size ()
    3. Os. Open ("."). Readdir (-1)//This code is a little bit too low.
    4. Directory traversal using the package Path/filepath :
      1. 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?
  4. container/list://double linked list?? Is there a higher level of data structure
  5. Sort. Sort
    1. 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
  6. Hash and cryptography *
  7. Servers
    1. Tcp
      1. Listener, err: = Net. Listen ("TCP", ": 9999")
      2. For {con, err: = Listener. Accept ()//hmm? Must you receive a connection to receive the next one?
      3. ERR: = Gob. Newdecoder (c). Decode (&msg)//var msg string; What is the codec algorithm for GOB?
      4. Client:
        1. C, Err: = Net. Dial ("TCP", "127.0.0.1:9999")
    2. HTTP
      1. http. Handlefunc ("/hello", hello)//func Hello (res http. Responsewriter, req *http. Request) {...}
      2. http. Listenandserve (": 9000", nil)
      3. Static file: http. Handle ("/assets/", http. Stripprefix ("/assets/", http. Fileserver (http. Dir ("assets")),)//Here is not a few more commas?
    3. RPC (slightly)
  8. Parsing command-line arguments
    1. Flag. Parse ()

Testing

    1. _test.go
    2. Import "Testing"
      1. Func testaverage (t *testing. T) {... t.error (...) ... }
    3. $ go Test

Concurrent

    1. < create channel and pass it to goroutine>
    2. Select {
      1. Case <-time. After (time. Second)://Timeout
    3. c: = make (chan int, 1)//buffered channel is asynchronous (but exceeding the limit will still block it)
    4. Initiate an HTTP GET request: res, err: = http. Get (URL)//Is this too concise? (No settings requested for header)

Next Steps

    1. Read the code implementation under go pkg?
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.