【gopl读书笔记(1)】第一章 入门

【时间】2021.12.31

【题目】【gopl读书笔记(1)】第一章 入门

本专栏是go圣经gopl《The Go Programming Language》中文版的读书笔记和思维导图

目录

一、简介

二、一些重点图

1、go语言的起源

2、bufio库进行io的例子

3、web服务例子

4、go指令

三、思维导图


一、简介

本章主要通过helloworld讲述了go语言的基本使用,并通过一些案例介绍go的一些特性。

  1. 入门(go的代码结构)
  2. 命令行参数 os.Args
  3. 相关内置库的使用(os.Stdio/os.Open()/bufio库进行IO/image库画图/http库进行web开发等
  4. 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)
		}
	}
}

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.

三、思维导图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值