Go Programming Language 2

1、In Go, the sign of the remainder is always the same as the sign of the dividend, so -5%3 and -5%-3 are both -2. The behavior of / depends on whether its operands are integers, so 5.0/4.0 is 1.25, but 5/4 is 1 because integer

division truncates the result toward zero.

2、bitwise operator

  

  The &^ op erator is bit cle ar (AND NOT): in the expression z = x &^ y, each bit of z is 0 if the corresponding bit of y is 1; otherwise it equals the corresponding bit of x.

3、Printf 的高级用法。

  

  the [1] ‘‘adverbs’’ after % tell Printf to use the first operand over and over again. Second, the # adverb for %o or %x or %X tells Printf to emit a 0 or 0x or 0X prefix respectively.

4、Runes are print ed wit h %c, or with %q if quoting is desired:

  

5、math.NaN()

  Any comparison wit h NaN always yields false:

  

6、Complex Numbers

  

  3.141592i  & 2i is an im aginary literal, denoting a complex number wit ha zero real component:

  complex literal:

  

7、strings

  Either or both of the i and j operands may be omitted, in which case the defau lt values of 0 (the start of the string) and len(s) (its end) are assumed, respectively.

  

  Since strings are immutable, constructions that try to modify a string's data in place are not allowed:

  

  A raw string literal is written `...`, using backquotes instead of double quotes.

8、UTF-8

  The high-order bits of the first byte of the encoding for a rune indicate how many bytes follow. A high-order indicates 7-bit ASCII.

  

  No rune’s encoding is a substring of any other, or even of a sequence of others, so you can search for a rune by just searching for its bytes.

  The unicode package provides functions for working with individual runes , and the unicode/utf8 package provides functions for encoding and decoding runes as bytes using UTF-8.

  utf8.RuneCountInString() 可以获取一个 utf8 字符串中的字符个数。

  

  utf8.DecodeRuneInString(str) 可以解析出第一个utf8字符,如下:

  

  使用 range 遍历utf8字符串,range会自动调用 DecodeRuneInString()方法进行解码。

  

  因此,可以使用 range 来统计字符串长度。下面右图是左图的精简版,忽略了 range 的返回值。

  

  也可以调用 utf8.RuneCountInString(s) 来获得 utf8 字符串的长度。

  []rune(utf8_str) 可以返回 utf8 字符串的 unicode code point.

  

9、Because strings are immutable, building up strings incrementally can involve a lot of allocation and copying . In such cases, it's more efficient to use the type bytes.Buffer.

  basename() 移除路径、以及扩展名。

    

10、A string contains an array of bytes that, once created, is immutable. By contrast, the elements of a byte slice can be freely modified.

  Strings can be converted to byte slices and back again:

  

11、strconv.Itoa() 用于转换。

    

12、FormatInt and FormatUint can be used to format numbers in a different base.

  

  fmt.Printf verbs %b, %d, %u, and %x are often more convenient than Format functions,especially if we want to include additional information besides the number:

  

  strconv.Atoi()、strcov.ParseInt() 用于将str 转换为 int。

  

13、单个常量

  

  多个常量

  

14、const generator iota

  Below declares Sunday to be 0, Monday to be 1, and so on.

  

  As iota increments, each constant is assigned the value of 1 << iota, which evaluates to successive powers of two, each corresponding to a single bit.

  

15、Untyped Constants

  many constants are not committed to a particular type.

  The compiler represents these uncommitted constants with much greater numeric precision than values of basic types, and arithmetic on them is more precise than machine arithmetic; you may assume at least 256 bits of precision.

16、Arrays are homogeneous—their elements all have the same type—whereas structs are heterogeneous. Both arrays and structs are fixed size. In contrast, slices and maps are dynamic data structures that grow as values are added.

  Array 中所有元素必须有相关的 type。Array、struct 固定大小,slice、map 动态大小。

17、使用 array literal 初始化 array.

  

  上述代码,两个3重复了。所以也可以使用 := 以及 ... 来编译器推导array 长度。

  

  [3]int and [4]int are different types.

  

  array literal 中可以设定索引来定义。

  

  Below defines an array r with 100 elements, all zero except for the last, which has value -1.

  

18、array 的相等性比较,使用的是其内所有元素的相等性比较。

  

19、Slice

  Slices represent variable-length sequences whose elements all have the same type. A slice type is written []T, where the elements have type T; it looks like an array type without a size.

  Slicing beyond cap(s) causes a panic, but slicing beyond len(s) extends the slice, so the result may be longer than the original.

  Passing a slice to a function permits the function to modify the underlying array elements.

  通过 slice 可以修改 array中的元素。

  

  

20、A simple way to rotate a slice left by n elements is to apply the function three times, reverse first to the leading n elements, then to the remaining elements, and finally to the whole slice.

    

  Unlike arrays, slices are not comparable, so we cannot use == to test whether two slices contain the same elements. The only legal slice comp arison is against nil, as in:

  

  注意区分 slice 的定义和创建。定义一个slice,其值为nil;创建一个slice,其值为非nil。

  

  The built-in function make creates a slice of a specified element type, length, and capacity。

  

21、内置的 append(slice, value) 函数有可能会创建一个全新的slice。

 func appendInt(x []int, y int) []int{
var z[]int
zlen:=len(x)+
if zlen<=cap(x){
z = x[:zlen]
}else{
zcap:=zlen
if zcap<*len(x){
zcap=*len(x)
}
z=make([]int,zlen,zcap)
copy(z,x)
}
z[len(x)]=y
return z
}

  因数调用 append() 后,有可能会创建一个全新的 slice,所以通常会将 append() 函数赋值给回自己。如下:

  

  append() 可以添加超过一个元素。

  

22、slice 可以理解为如下结构

  

23、使用 ellipsis 可以实现变长参数。如:

  

24、In-Place Slice Techniques

  1)nonempty

  

  2)

25、slice can be used to implement a stack.

stack = append(stack, v) // push v
top:=stack[len(stack)-] // top of stack
stack = stack[:len(stack)-] // pop

  实现从 slice 中移除元素。

  

  And if we don’t need to preserve the order, we can just mov e the last element int o the gap:

  

26、maps

  In Go, a map is a reference to a hash table, and a map type is written map[K]V, where K and V are the types of its keys and values. All of the keys in a given map are of the same type, and all of the values are of the same type, but the keys need not be of the same type as the values.

    

  map literal:

  

  delete() 函数可以移除key。

  

  But a map element is not a var iable, and we cannot take its address:

  

  map[] 方法实际上返回两个元素,第二个元素指明本元素是否有值。
  

27、Go does not provide a set  type, but since the keys of a map are distinct, a map can serve this purpose.

  一个map的value可以是另一个map。

  

28、

29、

30、

Go Programming Language 2的更多相关文章

  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. Salesforce 自定义元数据类型

    自定义元数据类型的优点 Salesforce中的设定都是以元数据(Metadata)存在的.在Salesforce中,用户可以新建自定义对象.自定义字段等,这些数据结构都以元数据的形式存储在系统中.当 ...

  2. day 22

    Creativity requires the courage to let go of certainties. 创新需要勇气承担不确定性.

  3. [题解向] 正睿Round435

    10.14 Link 唔,这一场打得不好.获得了\(\rm 75pts/300pts\)的得分,但是居然可以获得\(\rm 27/69\)的名次,也不至于不满意--毕竟是真不会233 \(\rm T1 ...

  4. [E] Shiro 官方文档阅读笔记 The Reading Notes of Shiro's Offical Docs

    官方文档: https://shiro.apache.org/reference.html https://shiro.apache.org/java-authentication-guide.htm ...

  5. C++ 10进制, 16进制, ASCII码, 单字节与多字节的相互转换

    这些简单的转换是用的比较频繁的, 因此将这些功能全部封装在一个类中 头文件 #pragma once #include <stdlib.h> #include <string> ...

  6. 团队作业第五次—项目冲刺-Day2

    Day2 part1-SCRUM: 项目相关 作业相关 具体描述 所属班级 2019秋福大软件工程实践Z班 作业要求 团队作业第五次-项目冲刺 作业正文 hunter--冲刺集合 团队名称 hunte ...

  7. C# API强制关机、重启以及注销计算机

    在Windows系统中有2种方式进行关机.重启以及注销计算机操作: 1.使用shutdown()命令:2.使用系统API: 以下是使用系统API进行操作的实例. 程序实例界面: 程序实例代码: 1 u ...

  8. C# 使用ConcurrentBag类处理集合线程安全问题

    在日常的开发中,经常会遇到多个线程对同一个集合进行读写操作,就难免会出现线程安全问题. 以下代码,如果使用List<T>就会遇到问题:System.InvalidOperationExce ...

  9. Istio开启mtls请求503问题分析

    背景 为测试Istio流量管理,将两个服务sleep.flaskapp的两个版本v1.v2(部署文件见参考链接)部署到Istio环境中,通过sleep-v1向flaskapp发起调用http://fl ...

  10. 转: 彻底理解 Spring 容器和应用上下文

    本文由 简悦 SimpRead 转码, 原文地址 https://mp.weixin.qq.com/s/o11jVTJRsBi998WlgpfrOw 有了 Spring 之后,通过依赖注入的方式,我们 ...