https://github.com/golang/go/wiki/LearnConcurrency

实例1:

package main

import (
"fmt"
"time"
) func say(s string) {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
} func main() {
go say("world")
say("hello")
}

 输出:

world
hello
hello
world
world
hello
hello
world
hello

  可以看到只输出了4个world就退出了,因为main执行完say("hello")就退出了

修改如下:

package main

import (
"fmt"
"time"
) func say(s string) {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
} func main() {
go say("world")
var input string
fmt.Scanln(&input) //程序停止执行,等待用户输入
}

  输出:

world
world
world
world
world
end //输入的string

  world完整输出了

golang ---Learn Concurrency的更多相关文章

  1. Golang 并发concurrency

    并发concurrency 很多人都是冲着Go大肆宣扬的高并发而忍不住跃跃欲试,但其实从源码解析来看,goroutine只是由官方实现的超级"线程池"而已.不过话说回来,每个实例4 ...

  2. Golang Learn Log #0

    Print/Printf 区别 Print: 可以打印出字符串, 和变量 fmt.Println(var) //right fmt.Println("string") //righ ...

  3. Netty实现高性能IOT服务器(Groza)之精尽代码篇中

    运行环境: JDK 8+ Maven 3.0+ Redis 技术栈: SpringBoot 2.0+ Redis (Lettuce客户端,RedisTemplate模板方法) Netty 4.1+ M ...

  4. Go语言的9大优势和3大缺点, GO语言最初的定位就是互联网时代的C语言, 我为什么放弃Go语言

    Go语言的9大优势和3大缺点 转用一门新语言通常是一项大决策,尤其是当你的团队成员中只有一个使用过它时.今年 Stream 团队的主要编程语言从 Python 转向了 Go.本文解释了其背后的九大原因 ...

  5. goroutine 分析 协程的调度和执行顺序 并发写

    package main import ( "fmt" "runtime" "sync" ) const N = 26 func main( ...

  6. MIT 6.824学习笔记2 RPC/Thread

    本节内容:Lect 2   RPC and Threads 线程:Threads allow one program to (logically) execute many things at onc ...

  7. goroutine 分析 协程的调度和执行顺序 并发写 run in the same address space 内存地址 闭包 存在两种并发 确定性 非确定性的 Go 的协程和通道理所当然的支持确定性的并发方式(

    package main import ( "fmt" "runtime" "sync" ) const N = 26 func main( ...

  8. Learn golang: Top 30 Go Tutorials for Programmers Of All Levels

    https://stackify.com/learn-go-tutorials/ What is Go Programming Language? Go, developed by Google in ...

  9. golang语言中的context详解,Go Concurrency Patterns: Context

    https://blog.golang.org/context Introduction In Go servers, each incoming request is handled in its ...

随机推荐

  1. Mysql 主从报错:1141

    主从同步,从库报错代码:1141 ,错误信息如下: Master_Port: 3306 Connect_Retry: 60 Master_Log_File: binlog.000086 Read_Ma ...

  2. MySQL数据库(一)-- 数据库介绍、MySQL安装、基础SQL语句

    一.数据库介绍 1.什么是数据库 数据库即存储数据的仓库 2.为什么要用数据库 (1)用文件存储是和硬盘打交道,是IO操作,所以有效率问题 (2)管理不方便 (3)一个程序不太可能仅运行在同一台电脑上 ...

  3. 重新编译kubeadm,修改默认证书时间

    参考 kubeadm alpha certs renew Kubeadm1.14 证书调整 kubeadm 部署的 kubernetes 集群,默认的证书有效时间是1年,需要每年手工更新. 1. 重新 ...

  4. 豆瓣读书isbn 查询

    最近学习微信小程序,做一个类似"书库"的小demo,大致流程使用摄像头获取书本后面的isbn,通过豆瓣读书API得到书本介绍.豆瓣评分.图书评论等信息,然鹅https://api. ...

  5. 团队第四次作业:alpha1发布成绩汇总

    一.作业题目 团队第四次作业:alpha1发布 二.作业评分标准 博客评分规则(总分100)博客要求 给出开头和团队成员列表(10') 给出发布地址以及安装手册(20') 给出测试报告(40') 给出 ...

  6. opencv 程序

    IplImage结构中的一个元素:struct _IplROI *roi; //图像感兴趣区域,当该值非空时,只对该区域进行处理 .   ROI :Region of Interest,表示感兴趣的区 ...

  7. 使用WIFI网卡 dhcp动态获取IP

    前面几篇博客中,wifi网卡的ip都是手工设置的,本篇博客将来移植dhcp,使得wifi网卡可以动态的获取ip.路由等信息. 那我们去哪里下载dhcp源码呢?在pc机上执行dh +tab键,看一下有哪 ...

  8. 百度快排发包python核心源码

    本源码仅供测试,发包有风险,优化还是踏实的好!本代码是本人自己学习python练手作品!  附上代码: # -*- coding: utf-8 -*-from selenium import webd ...

  9. idea安装、配置及基本使用

    下载 下载地址:https://www.jetbrains.com/idea/download/#section=windows Ultimate为旗舰版,功能全面,插件丰富,按年收费. Commun ...

  10. ManiFest.MF

    Manifest-Version: 1.0 //指定manifest文件的版本信息 Bundle-ManifestVersion: 2 //指定该包遵从 OSGi 规范 V3 或者 OSGi 规范 V ...