当前位置:网站首页>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}
边栏推荐
- Five design details of linear regulator
- Halcon blob analysis (ball.hdev)
- 猜谜语啦(9)
- TypeScript手把手教程,简单易懂
- Is the security account given by Yixue school safe? Where can I open an account
- [daily training -- Tencent selected 50] 557 Reverse word III in string
- Example 006: Fibonacci series
- Guess riddles (6)
- Basic number theory - factors
- 【三层架构及JDBC总结】
猜你喜欢

Guess riddles (11)

Daily question - input a date and output the day of the year

One question per day - replace spaces

Halcon blob analysis (ball.hdev)

Numpy pit: after the addition of dimension (n, 1) and dimension (n,) array, the dimension becomes (n, n)

319. Bulb switch

Xrosstools tool installation for X-Series

Arduino burning program and Arduino burning bootloader

Example 007: copy data from one list to another list.

实例004:这天第几天 输入某年某月某日,判断这一天是这一年的第几天?
随机推荐
Hello everyone, welcome to my CSDN blog!
Guess riddles (2)
猜谜语啦(142)
Mathematical modeling: factor analysis
Go dependency injection -- Google open source library wire
剑指 Offer 09. 用两个栈实现队列
Example 006: Fibonacci series
STM32 --- serial port communication
Halcon snap, get the area and position of coins
Typical low code apaas manufacturer cases
[牛客网刷题 Day4] JZ32 从上往下打印二叉树
The first week of summer vacation
STM32---ADC
STM32 summary (HAL Library) - DHT11 temperature sensor (intelligent safety assisted driving system)
轮子1:QCustomPlot初始化模板
Lori remote control commissioning record
Guess riddles (142)
Classification of plastic surgery: short in long long long
Matlab tips (28) fuzzy comprehensive evaluation
Example 008: 99 multiplication table