读书笔记

163 阅读6分钟
  • Go is not an object-oriented programming language,but it can mimic some of the functionality offered by object-programming languages,such as Java, Python, and C++

  • Usually, most system log files of a UNIX system can be found under the /var/log directory 通常Unix系统里,系统日志都在存储在/var/log目录下

  • The main idea behind generics in Go, as well as any other programming language that supports generics, is not having to write special code for supporting multiple data types when performing the same task. Currently, Go supports multiple data types in functions such as fmt.Println() 和其他语言一样,go里的泛型,是为了针对多种数据类型执行同样的任务而不用写一些定制化的代码,例如go里面的原生方法 fmt.Println() 就支持多种数据类型

  • I believe that generics should be used when they can create simpler code and designs. It is better to have repetitive straightforward code than optimal abstractions that slow down your applications 代码性能比写的好看更重要

  • Atoi stands for ASCII to Int

  • fmt.Printf("Type of x: %T\n", x) %T可以打印类型

  • Rune 本质是一个int32的类型,可以通过%c把rune类型作为字符打印

创建一个类型变量

a := ‘q’    单引号代表这是一个rune类型,

Pasted Graphic 3.png

The %c control string in fmt.Printf() prints a rune as a character.   不转换打印的话就是一个int32的值

  • 两种把整形转换成字符串的方式

string() 会把整形当做Unicode编码,是一个单字符

strconv. FormatInt() and strconv.Itoa()   会直接把整形转换成同样个数的字符

fune eSIAOK.png

  • As Go does not have a char data type, it uses byte and rune for storing character values. A single byte can only store a single ASCII character whereas a rune can store Unicode characters. However, a rune can occupy multiple bytes.

  • when the capacity of the slice changes, the connection to the array ceases to exist! This happens because when the capacity of a slice changes, so does the underlying array, and the connection between the slice and the original array does not exist anymore. 所有有些占用部分array的slice会脱离之前的空间,自力更生

  • 如果两者长度不一致,会出现数据缺失或者脏数据问题

copy(destination, input).png

Byte 是不带符号的8位整形,为了和8位整形无符号数区分,写了这个别名

fet.printr( Iemut ayte 1s st.brte(rand.iatssn( 699)).png

W byte 1s an al1as for uints and 1s eovlvalent to uints In alt naYs. It 1s.png

New会实现的细节

Using the new keyword.png

 

`^[-+]?\d+$`  用来匹配数字,^ 开头 $结尾 -+最多带一个 后面至少有一个数字
  • CSV读写文件
func main(){

//读文件

f,err := os.Open("/Users/huangshan/Downloads/批量发放星光模板 (1).csv")

if err != nil {

fmt.Println("open failed",err)

return

}

defer f.Close()

//res,err := csv.NewReader(f).Read() 一行一行来输出

res,err := csv.NewReader(f).ReadAll()

if err != nil {

fmt.Println("read failed ",err)

return

}

fmt.Println("res ",res)

//写文件

f,_ = os.Create("/Users/huangshan/Downloads/批量发放星光模板 (1).csv")

defer f.Close()

writer := csv.NewWriter(f)

_ = writer.Write([]string{"1","1","3","guan"})

writer.Flush()
}
  • 映射reflect的缺点

1.不易读

2.耗时多,动态的判断数据类型

3.有些错误不能在构建的时候捕捉到,错误触发的时间未知

Reflection code can look unpleasant and hard to read sometimes.png

  • 排序算法 操作的基础数据结构都是slice

You need to have a slice because all sorting operations work on slices

  • Reflect提供两种能力(获取数据的类型和值),一个是通过 type Switch 来获取interface{}背后数据的数据类型,然后根据数据类型做后续操作,二是 通过类型判断,获取到interface背后数据的真正的值,如果类型不一致,会panic

As explained, trying to extract the concrete.png

判断类型

func rOinterfacef}{.png

获取类型值
如果不判断是否成功直接获取值 ,需要肯定t的类型就是string,类型不一致会panic 带判断不会panic

k= t.(int)会panic,因为不是int类型.png

  • Json 内容可以解析成map[string]interface{},原来的数据类型仍然保留,不会丢失

Pasted Graphic 15.png

will be fine. However, there are times when you might get ane.png

image.png

  • Remember that UNIX considers everything, even a printer or your mouse, as a file. UNIX uses file descriptors, which are positive integer values, as an internal representation for accessing open files, which is much prettier than using long paths. So, by default, all UNIX systems support three special and standard filenames: /dev/stdin, /dev/stdout, and /dev/stderr, which can also be accessed using file  descriptors 0, 1, and 2, respectively.

  • Each running UNIX process is uniquely identified by an unsigned integer, which is called the process ID of the process.

  • Some signals cannot be caught, and the operating system cannot ignore them. So, the SIGKILL and SIGSTOP signals cannot be blocked, caught, or ignored and the reason for this is that they allow privileged users as well as the UNIX kernel to terminate any process they desire 有些信号是不能被捕捉的,系统也不能忽略这些信号。比如kill 信号等,这些信号可以让系统终止他们指定的进程。不能被捕捉的意思应该就是进程直接就被杀死了,根本没有机会捕捉吧

  • The runtime.Compiler variable holds the compiler toolchain used for building the running binary. The two most well-known values are gc and gccgo

总结: 这个书讲的比较浅显,像channel只会讲一些表现,不会深入讲他的底层构造,而且还有一些错误,比如这里,应该是关闭,或者有数据进来,都不会阻塞,协程会变成成可运行状态,继续往下走,而书里只说了关闭状态

image.png

  • gRPC is an open source remote procedure call (RPC) system that was developed at Google back in 2015, is built on top of HTTP/2, allows you to create services easily, and uses protocol buffers as the IDL which specifies the format of the interchanged messages and the service interface grpc是一个开源的远程程序调用,底层是http2,利用protocol buffer作为接口定义语言

  • grpc不需要server和client是同一个语言实现的

  • A protocol buffer (protobuf) is basically a method for serializing structured data. protobuf是一种序列化结构化数据的方法

  • Starting with Go 1.16, go install is the recommended way of building and installing packages in module mode. The use of go get is deprecated. However, when using go install, do not forget to add @latest after the package name to install the latest version.

option go_package = "cpm";

是用来指定生成的pb.go文件的package包名的

image.png

  • 同一个目录下不能存在两个不同的包名

  • interface vs generic interface 不能限制数据类型,所有数据类型都可能进入到方法中,type Switch 方法用来判断interface的真正类型,意味着未来每多一种类型就需要增加case,写多余的代码,不易维护
    generic 能限制数据类型,并且代码简单,但是在给编译和运行时带来很多工作,增加执行时间,性能下降

  • In C++, when you create new variables using the new operator, you know that these variables are going to the heap. This is not the case with Go and the use of the new() and make() functions. In Go, the compiler decides where a new variable is going to be placed based on its size and the result of escape analysis. This is the reason that you can return pointers to local variables from Go functions go里变量存放在堆还是栈上取决于变量占用的空间和逃逸分析的结果,这也是为什么我们能吧一个局部变量作为指针返回

  • The stack is the place where programming languages store temporary variables used by functions—each function has its own stack 栈是用来存储程序的局部变量的,每一个方法都有自己的栈