当前位置:网站首页>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}
边栏推荐
- Basic number theory - fast power
- Halcon Chinese character recognition
- U8g2 drawing
- Task failed task_ 1641530057069_ 0002_ m_ 000000
- Dynamic dimensions required for input: input, but no shapes were provided. Automatically overriding
- 容易混淆的基本概念 成员变量 局部变量 全局变量
- Configuration and startup of kubedm series-02-kubelet
- [Niuke brush questions day4] jz55 depth of binary tree
- ROS learning 1- create workspaces and function packs
- Illustrated network: what is gateway load balancing protocol GLBP?
猜你喜欢

Guess riddles (2)

猜谜语啦(8)

Guess riddles (11)

Programming implementation of ROS learning 5-client node

An enterprise information integration system

Explore the authentication mechanism of StarUML

Guess riddles (142)

Arduino burning program and Arduino burning bootloader

猜谜语啦(7)

How apaas is applied in different organizational structures
随机推荐
Business modeling | process of software model
猜谜语啦(4)
Use arm neon operation to improve memory copy speed
Typical low code apaas manufacturer cases
C [essential skills] use of configurationmanager class (use of file app.config)
Program error record 1:valueerror: invalid literal for int() with base 10: '2.3‘
Programming implementation of subscriber node of ROS learning 3 subscriber
[牛客网刷题 Day4] JZ55 二叉树的深度
golang 基础 —— golang 向 mysql 插入的时间数据和本地时间不一致
kubeadm系列-00-overview
皮尔森相关系数
Run menu analysis
Low code platform | apaas platform construction analysis
使用arm Neon操作,提高内存拷贝速度
It cold knowledge (updating ing~)
MPSoC QSPI Flash 升级办法
TypeScript手把手教程,简单易懂
Redis implements a high-performance full-text search engine -- redisearch
Ecmascript6 introduction and environment construction
Illustrated network: what is gateway load balancing protocol GLBP?