【Go Programming Language 3】html
一、These two statements declare a struct type called and a variable called that Employee dilbert is an instance of an Employee:shell
二、structjson
variable 使用 dot 來訪問成員,pointer 也能夠使用 dot 來訪問成員。api
The last statement is equivalent to:服務器
三、struct 定義時,同類型的 member 能夠合爲同一行ide
四、struct literal函數
若是member爲小寫,則在另外一個 package中,不能使用 struct literal 賦值。如:oop
五、Struct Embedding and Anonymous Fieldsui
正常的 Struct,按下左圖聲明,按下右圖使用。spa
在 Go中,能夠使用 Anonymous Struct Field,以下圖左定義,以下圖右使用。使用上比正常的Struct更加簡潔。
使用 Anonymous Fields 的 Struct 沒法使用普通的 Struct Literal,因此下面的 literal 都會引發編譯錯誤。
Anonymous Struct Literal 須像下面這樣定義。
六、JSON
Go has excellent support for encoding and decoding thesefor mats, provided by the standard library packages encoding/json, encoding/xml, encoding/asn1, and so on.
使用 json.Marshal() 函數能夠將 struct 轉換爲 JSON Data。下面左圖中的 json字符串爲 field tag。
a field tags. A field tag is a string of metadata associated at compile time with the field of a struct:
json.MarshalIndent() 函數能夠返回可視化的排版結果。
Only exported fields are marshaled, which is why we chose capitalized names for all the Go field names.
json.Unmarshal() 用於解包,而且能夠部分解包。
七、Text and HTML Templates
text/template、html/template packages, which provide a mechanism for substituting the values of variables into a text or HTML template.
{{range .Items}} and {{end}} actions create a loop, so the text between them is expanded multiple times, with dot bound to successive elements of Items.
the | notation makes the result of one operation the argument of another, analogous to a Unix shell pipeline.
建立一個Template時,能夠設置函數環境變量。
建立後,使用 Execute() 方法,便可執行一次template。
類型 template.HTML 會將本身直接插入進template,而 string 則會將本身做爲 string 插入 template。
八、函數定義
a sequence of parameters or results of the same type can be factored so that the type itself is written only once.
Here are four ways to declare a function with two parameters and one result
九、io.EOF 的使用
十、variadic function
經過 slice... 能夠用於解包 slice
func f(...int)、func g([]int) 與 [] int是兩個不一樣類型的函數。
...interface{} 用於代表接受任意參數。
十一、defer
a defer statement is an ordinary function or method call prefixed by the keyword defer. The actual call is deferred until the function that contains the statement defer has finished.
The right place for a statement that releases a resource is defer immediately after the resource has been successfully acquired.
使用 defer 標記函數的開始與結束時間。
A deferred anonymous function can even change the values that the enclosing function returns to its caller:
十二、Methods with a Pointer Receiver
method declarations are not permitted on named types that are themselves pointer types:
值調用、指針調用規則 ,共有4種狀況:
一、指針調用指針、值調用值確定是OK的。
二、值調用指針,分狀況。
1)可尋址變量,編譯器會將 val.x 改寫爲 (&val).x
2)不可尋址變量,沒法調用。
三、指針調用值
永遠OK,由於從地址老是能獲取到value。
1三、Method Value
receiver.Method,返回的值叫 methodValue,是一個跟receiver綁定的函數。
Method Expression
T.Method,返回的值叫 Method Expression,第一個參數爲 T receiver。
1四、Composing Types by Struct Embedding
嵌入一個匿名Struct後,其Method也被嵌入了。
1五、interface
1六、embedding an interface
1七、若是一個類型有如下方法。
func (*T) Close(),
則只有 *T 能夠賦值給io.Closer,而T不能賦值給 io.Closer(編譯器會報錯)。
1八、interface{} 是空接口,任意值都能賦給他。
1九、flag.Value
經過實現flag.Value,便可向flag庫中加入自定義的知識。
20、http服務器
1)ServeMux
2)DefaultServeMux
2一、Type Assertion
x.(T) 是類型檢查語法。x是一個接口類型。拿 x 的動態類型去對比,看是否等於T。
若是 T 是一個類型,則返回值爲 x 的動態Value。
if instead the asserted type is an interface type, then the e assertion checks whether x's dynamic type satisfies T.
若是T是接口,則返回值的動態類型、值不變,而接口類型變爲T。
Type Assertion 的功能本質是將 x 轉變爲 T。
2二、
2三、
2四、
2五、