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. idea svn 文件还原到指定版本

    使用svn经常会遇见,由于自己或者其他同事的提交(或者误操作),把原本正确代码或文件覆盖掉了,现在就得需要恢复到之前指定的某个版本. 先打开指定的文件,然后右上角点击时间的按钮: 这比会出现以前的许多 ...

  2. Shell 编程 文本处理工具 sed

    本篇主要写一些shell脚本文本处理工具sed的使用. 概述 sed(Stream EDitor)是一个强大而简单的文本解析转换工具,可以读取文本,并根据指定的条件对文本内容进行编辑(删除.替换.添加 ...

  3. 使用async进行结构化并发程序开发

    异步风格的函数: 继续来学习async相关的东东,对于它其实可以用到函数上,也就是用它可以定义一个异步风格的函数,然后在该函数中再来调用普通的函数,下面来瞅一下: 其实“GlobalScope.asy ...

  4. mysql基础知识之数据类型与约束

    一.约束 作用: 保证数据的完整性和一致性表的设计 1.not null 和 default not null 是放在最后用来约束 前面 数据类型的 (在原有基础上本来可以主键后面可以为空,但是一旦在 ...

  5. 线程三态和JVM线程状态

    1.线程三态:就绪态.运行态.阻塞态 2.JVM中的六种状态 NEW(新建状态):一个尚未启动的线程所处的状态. RUNNABLE(可运行状态):可运行线程的线程状态,可能正在运行,也可能在等待处理器 ...

  6. springCloud值Eureka

    Spring Cloud特点 约定优于配置 开箱即用.快速启动 适用于各种环境      PC Server  云环境  容器(Docker) 轻量级的组件  服务发现Eureka 组件的支持很丰富, ...

  7. Windbg Assembly Code(反汇编)窗口的使用

    在WinDbg中,可以通过输入命令(u, ub, uu (Unassemble))或使用反汇编窗口查看程序汇编代码. 如何打开 DissAssembly Code窗口 通过菜单View-->Di ...

  8. loj2245 [NOI2014]魔法森林 LCT

    [NOI2014]魔法森林 链接 loj 思路 a排序,b做动态最小生成树. 把边拆成点就可以了. uoj98.也许lct复杂度写假了..越卡常,越慢 代码 #include <bits/std ...

  9. PATB1002写出这个数

    参考代码: #include<cstdio> #include<cstring> #include<cstdlib> int main() { char str[1 ...

  10. nginx配置文件解释

    #全局配置 # For more information on configuration, see:# * Official English Documentation: http://nginx. ...