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:

  

2、struct

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

  

  

  The last statement is equivalent to:

  

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

  

4、struct literal

  

  

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

  

5、Struct Embedding and Anonymous Fields

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

    

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

    

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

  

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

  

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:

    

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

  

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

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

  

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.

  

  {{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。

  

  

8、函数定义

    

  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

  

9、io.EOF 的使用

  

10、variadic function

  

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

  

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

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

  

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.

  

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

  A deferred anonymous function can even change the values that the enclosing function returns to its caller:

  

12、Methods with a Pointer Receiver

  

  method declarations are not permitted on named types that are themselves pointer types:

  

  值调用、指针调用规则 ,共有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也被嵌入了。

  

  

15、interface

  

16、embedding an interface

  

  

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

  func (*T) Close(),

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

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

19、flag.Value

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

  

20、http服务器

  1)ServeMux

    

  2)DefaultServeMux

    

21、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。

22、

23、

24、

25、

Go Programming Language 3的更多相关文章

  1. iOS Swift-元组tuples(The Swift Programming Language)

    iOS Swift-元组tuples(The Swift Programming Language) 什么是元组? 元组(tuples)是把多个值组合成一个复合值,元组内的值可以使任意类型,并不要求是 ...

  2. iOS Swift-控制流(The Swift Programming Language)

    iOS Swift-控制流(The Swift Programming Language) for-in 在Swift中for循环我们可以省略传统oc笨拙的条件和循环变量的括号,但是语句体的大括号使我 ...

  3. iOS Swift-简单值(The Swift Programming Language)

    iOS Swift-简单值(The Swift Programming Language) 常量的声明:let 在不指定类型的情况下声明的类型和所初始化的类型相同. //没有指定类型,但是初始化的值为 ...

  4. Java Programming Language Enhancements

    引用:Java Programming Language Enhancements Java Programming Language Enhancements Enhancements in Jav ...

  5. The Swift Programming Language 英文原版官方文档下载

    The Swift Programming Language 英文原版官方文档下载 今天Apple公司发布了新的编程语言Swift(雨燕)将逐步代替Objective-C语言,大家肯定想学习这个语言, ...

  6. The Swift Programming Language 中文翻译版(个人翻新随时跟新)

    The Swift Programming Language --lkvt 本人在2014年6月3日(北京时间)凌晨起来通过网络观看2014年WWDC 苹果公司的发布会有iOS8以及OS X 10.1 ...

  7. [iOS翻译]《The Swift Programming Language》系列:Welcome to Swift-01

    注:CocoaChina翻译小组已着手此书及相关资料的翻译,楼主也加入了,多人协作后的完整译本将很快让大家看到. 翻译群:291864979,想加入的同学请进此群哦.(本系列不再更新,但协作翻译的进度 ...

  8. Questions that are independent of programming language. These questions are typically more abstract than other categories.

    Questions that are independent of programming language.  These questions are typically more abstract ...

  9. What is the Best Programming Language to Learn in 2014?

    It’s been a year since I revealed the best languages to learn in 2013. Once again, I’ve examined the ...

  10. C: Answers to “The C programming language, Edition 2”

    <The C programming language> Edition 2的习题答案地址: http://users.powernet.co.uk/eton/kandr2/index.h ...

随机推荐

  1. JAVA基础概念(三)

    JAVA方法入参和返回类型 方法入参 基础数据类型 引用数据类型 修饰符 返回类型 方法名(参数类型 参数名,参数类型 参数名...){//方法体return} 方法返回类型 return xxx 具 ...

  2. ASP.NET开发实战——(三)第一个ASP.NET应用《MyBlog》

    本文开始通过ASP.NET MVC创建一个博客应用,该应用是通过默认的MVC模板修改而来,所以创建的过程和代码都与默认模板一致,然后通过修改的方式将默认模板改为博客的主页,并添加博客列表.内容等页面. ...

  3. Xamarin.Forms移动开发系列5 :XAML标记扩展

    摘要 本文主要讲述Xamarin.Forms中XAML的标记扩展. 前言 在Xamarin.Forms移动开发系列4 :XAML基础一文中提到过XAML标记扩展,本文将对标记扩展进行更深入的了解. 大 ...

  4. 【转】Java 内部类总结

    Java内部类 一. 含义 在Java编程语言里,程序是由类(class)构建而成的.在一个类的内部也可以声明类,我们把这样的类叫做内部类. 二. 作用 实现了更好的封装,我们知道,普通类(非内部类) ...

  5. 第01组 Beta冲刺(1/5)

    队名:007 组长博客: https://www.cnblogs.com/Linrrui/p/11985569.html 作业博客: https://edu.cnblogs.com/campus/fz ...

  6. navicat 11.2.7破解

    1,软件安装包目录 2,根据电脑系统安装x64或者x86,安装完成之后将PatchNavicat.exe放到navicat的安装目录下 3,右键以管理员身份运行PatchNavicat.exe,或者双 ...

  7. 微信JSSDK 扫描二维码

    <?php require_once('wxjssdk.class.php'); $weixin = new class_weixin(); $signPackage = $weixin-> ...

  8. Intellij插件之MavenHelper

    作用: 一键查看maven依赖,查看冲突的依赖,一键进行exclude依赖 插件提供地址: https://plugins.jetbrains.com/plugin/7179-maven-helper ...

  9. Flink之state processor api原理

    无论您是在生产环境中运行Apache Flink or还是在过去将Flink评估为计算框架,您都可能会问自己一个问题:如何在Flink保存点中访问,写入或更新状态?不再询问!Apache Flink ...

  10. SQL ------------ 对表中字段的操作 alter

    ALTER TABLE 语句用于在现有表中添加.删除或修改列. 注意不同的数据库的语句有所不一样 增加和删除都基本一致. 进行修改表中字段的时候注意: 本文主要介绍  sqlserver/mysql/ ...