golang Golang Gotcha Rendezvous迭代

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang Golang Gotcha Rendezvous迭代相关的知识,希望对你有一定的参考价值。

package main

import "fmt"
import "sync"

func main() {
	twoNephews := []string{"Huey", "Dewey"}
	threeNephews := append(twoNephews, "Louie")

	var wg sync.WaitGroup

	for _, nephew := range threeNephews {
		wg.Add(1)
		go func() {
			fmt.Println("Hello", nephew)              // the current value of nephew isn't captured by the goroutine
			wg.Done()
		}()
	}

	// Wait for all greetings to complete.
	wg.Wait()
}

// Output:
// Hello Louie
// Hello Louie
// Hello Louie
package main

import "fmt"
import "sync"

func main() {
	twoNephews := []string{"Huey", "Dewey"}
	threeNephews := append(twoNephews, "Louie")

	var wg sync.WaitGroup

	for _, nephew := range threeNephews {
		wg.Add(1)
		go func(who string) {                       
			fmt.Println("Hello", who)
			wg.Done()
		}(nephew)
	}

	// Wait for all greetings to complete.
	wg.Wait()
}

// Output:
// Hello Louie
// Hello Huey
// Hello Dewey

golang如何打印内存内容

参考技术A golang如何打印内存内容

以上是关于golang Golang Gotcha Rendezvous迭代的主要内容,如果未能解决你的问题,请参考以下文章

Golang 学习之路

Golang 入门

Golang入门到项目实战 第一个golang应用

golang编译androidso无法加载

golang如何打印内存内容

Golang入门到项目实战 golang匿名函数