【时间】2021.12.31
【题目】【gopl读书笔记(1)】第一章 入门
本专栏是go圣经gopl《The Go Programming Language》中文版的读书笔记和思维导图
目录
一、简介
本章主要通过helloworld讲述了go语言的基本使用,并通过一些案例介绍go的一些特性。
- 入门(go的代码结构)
- 命令行参数 os.Args
- 相关内置库的使用(os.Stdio/os.Open()/bufio库进行IO/image库画图/http库进行web开发等)
- go指令(go run/build/get等)
二、一些重点图
1、go语言的起源
-
顺序通信进程 ( communicating sequential processes ) ,缩写为 CSP
-
C语言
2、bufio库进行io的例子
// Dup1 prints the text of each line that appears more than
// once in the standard input, preceded by its count.
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
counts := make(map[string]int)
input := bufio.NewScanner(os.Stdin)
for input.Scan() {
counts[input.Text()]++
}
// NOTE: ignoring potential errors from input.Err()
for line, n := range counts {
if n > 1 {
fmt.Printf("%d\t%s\n", n, line)
}
}
}
- bufio详见:深入理解 Go 标准库之 bufio.Scanner
3、web服务例子
- get请求( 并发获取多个URL)
// Fetchall fetches URLs in parallel and reports their times and sizes.
package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"time"
)
func main() {
start := time.Now()
ch := make(chan string)
for _, url := range os.Args[1:] {
go fetch(url, ch) // start a goroutine
}
for range os.Args[1:] {
fmt.Println(<-ch) // receive from channel ch,输出到stdout
}
fmt.Printf("%.2fs elapsed\n", time.Since(start).Seconds())
}
func fetch(url string, ch chan<- string) {
start := time.Now()
resp, err := http.Get(url)
if err != nil {
ch <- fmt.Sprint(err) // send to channel ch
return
}
nbytes, err := io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close() // don't leak resources
if err != nil {
ch <- fmt.Sprintf("while reading %s: %v", url, err)
return
}
secs := time.Since(start).Seconds() //time.Since(time.Time)从某一时刻开始到现在的时间
ch <- fmt.Sprintf("%.2fs %7d %s", secs, nbytes, url)
}
- web服务器(路径与handler匹配)
// Server1 is a minimal "echo" server.
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
http.HandleFunc("/", handler) // each request calls handler
log.Fatal(http.ListenAndServe("localhost:8000", nil))
}
// handler echoes the Path component of the requested URL.
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "URL.Path = %q\n", r.URL.Path)
}
//!-
4、go指令
#1、go指令
Usage:
go <command> [arguments]
The commands are:
bug start a bug report #启动bug 报告
build compile packages and dependencies # 编译,生成.exe文件
clean remove object files and cached files # 清除对象文件和缓存文件
doc show documentation for package or symbol # 显示package或symbol的文档
env print Go environment information # 显示和设置环境信息
fix update packages to use new APIs
fmt gofmt (reformat) package sources #format格式化代码
generate generate Go files by processing source #根据正在运行的源码生成go文件
get add dependencies to current module and install them #新增依赖到当前模块并安装
install compile and install packages and dependencies # 编译并安装包package和依赖
list list packages or modules # 列出包或模块
mod module maintenance
run compile and run Go program # 编译并运行go项目
test test packages # 测试package
tool run specified go tool
version print Go version
vet report likely mistakes in packages
Use "go help <command>" for more information about a command.