我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

Golang。 通道阵列

I have an assignment to use arrays of channels.

I do not understand. Why this does not work?

package main

import "fmt"

func run() {
    chann[0] <- 1
}

var chann = make([]chan int, 2)

func main() {
    go run()
    obj := <- chann[0]
    fmt.Println(obj)
}

You have initialized the array of channels, but not the channels themselves.

As for why it deadlocks; this is due to the fact that a channel value that has not been initalised, or has been set to nil will always block. (See this article)

近期文章

  • Go中的UDP服务器/客户端无法读取数据
  • BoltDB作为后端数据库的性能
  • gob.Register方法的目的是什么?
  • 在golang中构建没有github的本地导入
  • Golang yacc生成的解析器的访问结果
  • 如何确保kubernetes的平稳扩展?
  • Golang:如何在没有cgo的情况下调用win32 API?
  • 从golang包中导出接口而不是结构
  • Golang:为什么os.Exit在goroutines中不起作用
  • goroutine总是执行“后进先出”
  • 使用Go登录到Google Container / Compute Engine中的Google Cloud
  • 重新定义Golang标志
  • Go中的Quicksort
  • 从Pem字符串Golang生成私钥
  • 怎么做Grafana代码调试?
  • D中是否有等效的goroutine?
  • Go代码在go测试和go运行中的行为有所不同
  • Go-如何将包含十六进制字符的txt文件解码/转换为可读字符串
  • 查找查找版本[重复]
  • 测试Golang Goroutine

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华