当前位置:网站首页>golang 基础 ——map、数组、切片 存放不同类型的数据
golang 基础 ——map、数组、切片 存放不同类型的数据
2022-07-05 08:41:00 【猎人在吃肉】
基础知识,不解释,直接看代码
package main
import (
"fmt"
)
type User struct {
ID string
Name string
Age int
}
func main() {
fmt.Println("------------------- map -------------------------")
data := map[string]interface{
}{
} // 注意:是2个大括号
data["num"] = 123 // int 类型
data["str"] = "helloworld" // 字符串类型
user := &User{
ID: "1001", Name: "zhangsan", Age: 18}
data["user1"] = user // User对象类型
for k, v := range data {
fmt.Printf("k= %v , v的类型是 %T ,v= %v \n", k, v, v)
}
fmt.Println("----------------- 数组 ---------------------")
var paramters []interface{
} // 定义切片,注意:有1个大括号
paramters = append(paramters, 456) // int 类型
paramters = append(paramters, "李四") // 字符串类型
paramters = append(paramters, user) // User对象类型
for k, v := range paramters {
fmt.Printf("k= %v , v的类型是 %T ,v= %v \n", k, v, v)
}
}
运行结果:
------------------- map -------------------------
k= num , v的类型是 int ,v= 123
k= str , v的类型是 string ,v= helloworld
k= user1 , v的类型是 *main.User ,v= &{
1001 zhangsan 18}
----------------- 数组 ---------------------
k= 0 , v的类型是 int ,v= 456
k= 1 , v的类型是 string ,v= 李四
k= 2 , v的类型是 *main.User ,v= &{
1001 zhangsan 18}
边栏推荐
- 696. 计数二进制子串
- Guess riddles (3)
- Guess riddles (7)
- Old Wang's esp8266 and old Wu's ws2818 light strip
- Guess riddles (5)
- Apaas platform of TOP10 abroad
- Bluebridge cup internet of things competition basic graphic tutorial - clock selection
- Guess riddles (6)
- Business modeling of software model | object modeling
- Some pitfalls of win10 network sharing
猜你喜欢
随机推荐
实例010:给人看的时间
Sword finger offer 09 Implementing queues with two stacks
Dynamic dimensions required for input: input, but no shapes were provided. Automatically overriding
实例004:这天第几天 输入某年某月某日,判断这一天是这一年的第几天?
Numpy pit: after the addition of dimension (n, 1) and dimension (n,) array, the dimension becomes (n, n)
Warning: retrying occurs during PIP installation
My university
每日一题——输入一个日期,输出它是该年的第几天
Guess riddles (4)
Tips 1: Web video playback code
Cmder of win artifact
L298N module use
Guess riddles (11)
C# LINQ源码分析之Count
[NAS1](2021CVPR)AttentiveNAS: Improving Neural Architecture Search via Attentive Sampling (未完)
C language data type replacement
Halcon snap, get the area and position of coins
One dimensional vector transpose point multiplication np dot
Bit operation related operations
99 multiplication table (C language)









