• golang学习笔记 --- goroutine


    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
    	}
    	fmt.Printf("%.2fs elapsed
    ", 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()
    	ch <- fmt.Sprintf("%.2fs  %7d  %s", secs, nbytes, url)
    }

    goroutine是一种函数的并发执行方式,而channel是用来在goroutine之间进行参数传递。
    main函数本身也运行在一个goroutine中,而go function则表示创建一个新的goroutine,并在
    这个新的goroutine中执行这个函数。

    main函数中用make函数创建了一个传递string类型参数的channel,对每一个命令行参数,我
    们都用go这个关键字来创建一个goroutine,并且让函数在这个goroutine异步执行http.Get方
    法。这个程序里的io.Copy会把响应的Body内容拷贝到ioutil.Discard输出流中(译注:可以把
    这个变量看作一个垃圾桶,可以向里面写一些不需要的数据),因为我们需要这个方法返回
    的字节数,但是又不想要其内容。每当请求返回内容时,fetch函数都会往ch这个channel里写
    入一个字符串,由main函数里的第二个for循环来处理并打印channel里的这个字符串。

    当一个goroutine尝试在一个channel上做send或者receive操作时,这个goroutine会阻塞在调
    用处,直到另一个goroutine往这个channel里写入、或者接收值,这样两个goroutine才会继续
    执行channel操作之后的逻辑。在这个例子中,每一个fetch函数在执行时都会往channel里发
    送一个值(ch <- expression),主函数负责接收这些值(<-ch)。这个程序中我们用main函数来接
    收所有fetch函数传回的字符串,可以避免在goroutine异步执行还没有完成时main函数提前退
    出。

  • 相关阅读:
    配置ADCS后访问certsrv的问题
    CSS基本知识
    HTML的基本架构
    网站设计需要的基本知识
    系统安装Serv-U后IIS出错提示:HRESULT:0x80070020
    服务器环境配置全过程
    服务器、客户端双认证(2)
    PHP 计数排序算法
    sql语句随机取一张的数据 rand()
    PHP导入CSV文件数据
  • 原文地址:https://www.cnblogs.com/saryli/p/9993082.html
Copyright © 2020-2023  润新知