Go Programming Language 3

1、These two statements declare a struct type called and a variable called that Employee dilbert is an instance of an Employee:

  Go Programming Language 3

2、struct

  variable 使用 dot 来访问成员,pointer 也可以使用 dot 来访问成员。

  Go Programming Language 3

  Go Programming Language 3

  The last statement is equivalent to:

  Go Programming Language 3

3、struct 定义时,同类型的 member 可以合为同一行

  Go Programming Language 3

4、struct literal

  Go Programming Language 3

  Go Programming Language 3

  如果member为小写,则在另一个 package中,不能使用 struct literal 赋值。如:

  Go Programming Language 3

5、Struct Embedding and Anonymous Fields

  正常的 Struct,按下左图声明,按下右图使用。

  Go Programming Language 3  Go Programming Language 3

  在 Go中,可以使用 Anonymous Struct Field,如下图左定义,如下图右使用。使用上比正常的Struct更加简洁。

  Go Programming Language 3  Go Programming Language 3

  使用 Anonymous Fields 的 Struct 无法使用普通的 Struct Literal,所以下面的 literal 都会引起编译错误。

  Go Programming Language 3

  Anonymous Struct Literal 须像下面这样定义。

  Go Programming Language 3

6、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:

  Go Programming Language 3  Go Programming Language 3

  json.MarshalIndent() 函数可以返回可视化的排版结果。

  Go Programming Language 3

  Only exported fields are marshaled, which is why we chose capitalized names for all the Go field names.

  json.Unmarshal() 用于解包,并且可以部分解包。

  Go Programming Language 3

7、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.

  Go Programming Language 3

  {{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时,可以设置函数环境变量。

    Go Programming Language 3

  创建后,使用 Execute() 方法,即可执行一次template。

  Go Programming Language 3

  类型 template.HTML 会将自己直接插入进template,而 string 则会将自己作为 string 插入 template。

  Go Programming Language 3

  Go Programming Language 3

8、函数定义

  Go Programming Language 3  

  a sequence of parameters or results of the same type can be factored so that the type itself is written only once.

  Go Programming Language 3

  Here are four ways to declare a function with two parameters and one result

  Go Programming Language 3

9、io.EOF 的使用

  Go Programming Language 3

10、variadic function

  Go Programming Language 3

  通过 slice... 可以用于解包 slice

  Go Programming Language 3

  func f(...int)、func g([]int) 与 [] int是两个不同类型的函数。

  ...interface{} 用于表明接受任意参数。

  Go Programming Language 3

11、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.

  Go Programming Language 3

  使用 defer 标记函数的开始与结束时间。
  Go Programming Language 3

  returns to its caller:

  Go Programming Language 3

12、Methods with a Pointer Receiver

  Go Programming Language 3

  ted on named types that are themselves pointer types:

  Go Programming Language 3

  值调用、指针调用规则 ,共有4种情况:

  1、指针调用指针、值调用值肯定是OK的。

  2、值调用指针,分情况。

    1)可寻址变量,编译器会将 val.x 改写为 (&val).x

    2)不可寻址变量,无法调用。

  3、指针调用值

    永远OK,因为从地址总是能获取到value。

13、Method Value

  receiver.Method,返回的值叫 methodValue,是一个跟receiver绑定的函数。

  Method Expression

  T.Method,返回的值叫 Method Expression,第一个参数为 T receiver。

14、Composing Types by Struct Embedding

  嵌入一个匿名Struct后,其Method也被嵌入了。

  Go Programming Language 3

  Go Programming Language 3

15、interface

  Go Programming Language 3

16、embedding an interface

  Go Programming Language 3

  Go Programming Language 3

17、如果一个类型有以下方法。

  func (*T) Close(),

  则只有 *T 可以赋值给io.Closer,而T不能赋值给 io.Closer(编译器会报错)。

18、interface{} 是空接口,任意值都能赋给他。

19、flag.Value

  通过实现flag.Value,即可向flag库中加入自定义的知识。

  Go Programming Language 3

20、http服务器

  1)ServeMux

    Go Programming Language 3

  2)DefaultServeMux

    Go Programming Language 3

21、Type Assertion

  x.(T) 是类型检查语法。x是一个接口类型。拿 x 的动态类型去对比,看是否等于T。

  如果 T 是一个类型,则返回值为 x 的动态Value。

    Go Programming Language 3

  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。

22、

23、

24、

25、

 

相关文章: