编程语言爱好者Go语言实践

《Go in Action》第一章读书笔记

2019-10-19  本文已影响0人  Mr_Hospital

本文为《Go in Action》的第一章读书笔记。
第一章主要是对go语言的一个介绍。

Q: go语言想要解决的问题?

A: 原文:

The Go team went to great lengths to solve the problems facing software developers today. Developers have to make an uncomfortable choice between rapid development and performance when choosing a language for their projects. Languages like C and C++ offer fast execution, whereas languages like Ruby and Python offer rapid development.

简单来说,就是为了提供一门既能快速开发然后性能又好的语言。
有些语言,比如c和c++,性能高但是对开发人员的要求也高,也就不能快速开发。而Ruby和Python对开发人员很友好,但是性能却不怎么样。

Q: go有哪些特点?

A: 原文:

As we explore Go, you’ll find well-planned features and concise syntax. As a language, Go is defined not only by what it includes, but by what it doesn’t include. Go has a concise syntax with few keywords to memorize. Go has a compiler that’s so fast, sometimes you’ll forget it’s running. As a Go developer, you’ll spend significantly less time waiting for your project to build. Because of Go’s built-in concurrency features, your software will scale to use the resources available without forcing you to use special threading libraries. Go uses a simple and effective type system that takes much of the overhead out of object-oriented development and lets you focus on code reuse. Go also has a garbage collector, so you don’t have to manage your own memory.

简单来说,go语言具有精心设计的语言特性以及精简的语法。
其具有以下特点:

开发速度

编译一个大型c或c++应用的时间往往超过等待一杯咖啡的时间。go的编译时间缩短,依靠:

动态语言的效率高,是因为在代码编写和运行之间没有其余步骤,就是说写了代码就可以直接运行,不需要什么编译、链接之类的步骤。但是动态语言不能像静态语言那样保证类型安全。因此,很多时候需要写测试以保证代码的类型正确性。

在go语言中,编译器会检查类型匹配问题。

concurrency

并发。原文:

Go’s concurrency support is one of its strongest features. Goroutines are like threads, but use far less memory and require less code to use. Channels are data structures that let you send typed messages between goroutines with synchronization built in. This facilitates a programming model where you send data between goroutines, rather than letting the goroutines fight to use the same data.

go的并发支持是其最强的几个特性之一。
Goroutines跟线程有些相似,但是使用了更少的内存,也不需要那么多代码去编写。
Channels,是一种数据结构,用于在goroutines之间发送消息,同时内建了同步机制。这种方式构建了一种在goroutines之间发送数据的编程模式,而不是让几个goroutines去竞争使用同样的数据。

简单来说,go的并发模型是消息发送,而不是竞争去使用共享数据。

goroutines

原文:

Goroutines are functions that run concurrently with other goroutines, including the entry point of your program.
In Go, the net/http library has concurrency built in using goroutines. Each inbound request automatically runs on its own goroutine.
Goroutines use less memory than threads and the Go runtime will automatically schedule the execution of goroutines against a set of configured logical processors. Each logical processor is bound to a single OS thread
几点:

channels

原文:

Channels help to enforce the pattern that only one goroutine should modify the data at any time
It’s important to note that channels don’t provide data access protection between goroutines. If copies of data are exchanged through a channel, then each goroutine has its own copy and can make any changes to that data safely. When pointers to the data are being exchanged, each goroutine still needs to be synchronized if reads and writes will be performed by the different goroutines.

几点:

类型系统

原文:

Go developers simply embed types to reuse functionality in a design pattern called composition. Other languages use composition, but it’s often deeply tied to inheritance, which can make it complicated and difficult to use. In Go, types are composed of smaller types, which is in contrast to traditional inheritance-based models.

简单来说,go使用的是组合(composition),类型里面有更小的类型,而不是使用继承。

原文:

In addition Go has a unique interface implementation that allows you to model behavior, rather than model types.
You don’t need to declare that you’re implementing an interface in Go; the compiler does the work of determining whether values of your types satisfy the interfaces you’re using.
GO INTERFACES MODEL SMALL BEHAVIORS

go具有interface这个特性。但不一样的是,开发人员不必声明类型实现了哪个interface,编译器会自己判断。
go的interface的理念就是定义小的行为,注意要小。

内存管理

When you write code with garbage collection in mind, Go’s garbage collection adds little overhead to program execution time, but reduces development effort significantly. Go takes the tedium out of programming and leaves the bean counting to the accountants.

就简单介绍了一下,垃圾回收让开发者不必担心内存回收问题。

最简单的例子

本章最后给了一个最简单的例子:

package main

import "fmt"

func main() {
  fmt.Println("Hello World!")
}

然后在什么地方运行呢?书里面说在play.golang.org线上运行。

这里解释一下这段代码:

上一篇 下一篇

猜你喜欢

热点阅读