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

如何建立混合数组

In ruby I can created array is filled with types:

[1, 'hello', :world] # [Fixnum, String, Symbol]
=> [1, "hello", :here]

How to implement a similar array is filled with mixed types in Go?

How to declare the array?

You can do that via the empty interface - interface{}:

arr := make([]interface{}, 0)

arr = append(arr, "asdfs")
arr = append(arr, 5)

or in literal form:

arr := []interface{}{"asdfs", 5}

Whenever you want to use a value of that array you need to use a type assertion.

近期文章

  • 分叉存储库上的“ go build”问题
  • 在Go中检查结构的类型
  • 如何在Go中使用'multipart'设置表单的Content-Type
  • 您如何确定函数是否应该像接收器一样使用指针?
  • 当上下文变量超出golang的范围时,Context.Done()是否会解除阻止?
  • Golang程序在Windows 10中给出了不正确的结果
  • 获取已知结构字段的名称
  • Heroku Go应用程序崩溃
  • 动态创建某种类型的结构并将其解编成JSON
  • 为什么Go中带文字的浮点乘法与变量之间有区别?
  • Go中的grpc标头/ cookie
  • Golang如何用正则表达式组替换字符串? [重复]
  • 如何在go中将[] byte数据转换为uint16?
  • 如何在golang中转换日期格式?
  • 解组XML,其中包含未转义和字符
  • 在init或处理程序函数中读取模板?
  • Go-货币计算
  • 在golang中解析属性文件中的值
  • 无法理解界面/结构关系
  • 从goroutine获得价值并取消另一个goroutine

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

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

友情链接:代码精华