golang在函数中改变slice值

前端之家收集整理的这篇文章主要介绍了golang在函数中改变slice值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package main

import (
    "fmt"
    "time"
)

func main() {
    registerChan:=make(chan string)

    go func(){
        registerChan<-"hi"
    }()
    go func(){
        registerChan<-"hello"
    }()

    schedule(registerChan)

}


func schedule(registerChan chan string){
    workers := []string{}
    go func(wkrs *[]string){
        for{
            wkr := <-registerChan 
            *wkrs = append(*wkrs,wkr)
            }
    }(&workers)
    time.Sleep(4)
    fmt.Println(workers,len(workers))
}

猜你在找的Go相关文章