Creation and initialization of map in Go

Source: Internet
Author: User

// code_014_map_usage project main.gopackage mainimport ( "fmt")func main() { /*Go语言中的map(映射,字典)是一种内置的数据结构,它是一个无序的key-value对的集合。 1)键的约束: 在一个map里所有的键都是唯一的,而且必须是支持==和!=操作符的类型 2)切片、函数以及包含切片的结构类型这些类型由于具有引用语义,不能作为映射的键 err, invalid map key type []string 3)map值: 可以是任意类型,没有限制。 4)键值的数据类型: map里所有键的数据类型必须是相同的,值也必须如何,但键和值的数据类型可以不相同。 5)注意:map是无序的,我们无法决定它的返回顺序,所以,每次打印结果的顺利有可能不同。 */ //1、创建 var g1 map[int]string //默认值为nil g2 := map[int]string{} g3 := make(map[int]string) g4 := make(map[int]string, 10) fmt.Println(g1, g2, g3, g4, len(g4)) //2、初始化 var m1 map[int]string = map[int]string{1: "ck_god", 2: "god_girl"} m2 := map[int]string{1: "ck_god", 2: "god_girl"} fmt.Println(m1, m2)}

The results are as follows:

//map[] map[] map[] map[] 0//map[1:ck_god 2:god_girl] map[1:ck_god 2:god_girl]

Creation and initialization of map in Go

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.