当前位置:网站首页>Golang foundation -- map, array and slice store different types of data
Golang foundation -- map, array and slice store different types of data
2022-07-05 08:50:00 【The hunter is eating meat】
Basic knowledge of , Don't explain , Look directly at the code
package main
import (
"fmt"
)
type User struct {
ID string
Name string
Age int
}
func main() {
fmt.Println("------------------- map -------------------------")
data := map[string]interface{
}{
} // Be careful : yes 2 Two braces
data["num"] = 123 // int type
data["str"] = "helloworld" // String type
user := &User{
ID: "1001", Name: "zhangsan", Age: 18}
data["user1"] = user // User object type
for k, v := range data {
fmt.Printf("k= %v , v The type is %T ,v= %v \n", k, v, v)
}
fmt.Println("----------------- Array ---------------------")
var paramters []interface{
} // Defining slices , Be careful : Yes 1 Two braces
paramters = append(paramters, 456) // int type
paramters = append(paramters, " Li Si ") // String type
paramters = append(paramters, user) // User object type
for k, v := range paramters {
fmt.Printf("k= %v , v The type is %T ,v= %v \n", k, v, v)
}
}
Running results :
------------------- map -------------------------
k= num , v The type is int ,v= 123
k= str , v The type is string ,v= helloworld
k= user1 , v The type is *main.User ,v= &{
1001 zhangsan 18}
----------------- Array ---------------------
k= 0 , v The type is int ,v= 456
k= 1 , v The type is string ,v= Li Si
k= 2 , v The type is *main.User ,v= &{
1001 zhangsan 18}
边栏推荐
- C [essential skills] use of configurationmanager class (use of file app.config)
- Mengxin summary of LIS (longest ascending subsequence) topics
- 猜谜语啦(3)
- Guess riddles (8)
- Agile project management of project management
- 696. 计数二进制子串
- 猜谜语啦(142)
- Halcon Chinese character recognition
- Esp8266 interrupt configuration
- Classification of plastic surgery: short in long long long
猜你喜欢

猜谜语啦(11)

Pytorch entry record

Mathematical modeling: factor analysis

Halcon Chinese character recognition

猜谜语啦(5)

Agile project management of project management

Arduino burning program and Arduino burning bootloader

Guess riddles (142)

Ros- learn basic knowledge of 0 ROS - nodes, running ROS nodes, topics, services, etc

How apaas is applied in different organizational structures
随机推荐
Redis实现高性能的全文搜索引擎---RediSearch
ORACLE进阶(三)数据字典详解
ROS learning 1- create workspaces and function packs
Halcon blob analysis (ball.hdev)
GEO数据库中搜索数据
Guess riddles (8)
Guess riddles (3)
How many checks does kubedm series-01-preflight have
The first week of summer vacation
asp.net(c#)的货币格式化
kubeadm系列-00-overview
Lori remote control commissioning record
Run menu analysis
golang 基础 ——map、数组、切片 存放不同类型的数据
IT冷知识(更新ing~)
golang 基础 —— golang 向 mysql 插入的时间数据和本地时间不一致
Speech recognition learning summary
【日常训练--腾讯精选50】557. 反转字符串中的单词 III
猜谜语啦(4)
Array, date, string object method