A Tour of Go Map literals continued

If the top-level type is just a type name, you can omit it from the elements of the literal.

package main 

import "fmt"

type Vertex struct {
    Lat, Long float64
}
var m = map[string]Vertex {
    "Bell Labs": {40.68433,-74.39967},
    "Google": {37.42202, -122.08408},
}
func main() {
    var m2 map[int]uint8
    m2 = make(map[int]uint8)
    m2[12] = uint8(12)
    m2[1] = uint8(1)
    fmt.Println(m)
    fmt.Println(m2)
}
时间: 2024-08-09 10:32:26

A Tour of Go Map literals continued的相关文章

A Tour of Go Map literals

Map literals are like struct literals, but the keys are required. package main import "fmt" type Vertex struct { Lat, Long float64 } var m = map[string]Vertex { "Bell Labs": Vertex { 40.6833, -74.39967, }, "Google": Vertex{ 3

A Tour of Go Struct Literals

A struct literal denotes a newly allocated struct value by listing the values of its fields. You can list just a subset of fields by using the Name: syntax. (And the order of named fields is irrelevant.) The special prefix & constructs a pointer to a

golang基础练习(一)

//遍历map package main import "fmt" func main() { x := make(map[string]int) x["zhangsan"] = 3 x["lisi"] = 4 x["wangwu"] = 5 //#丢弃值 for i,_ := range x { fmt.Println(i) } } //匿名函数 package main import "fmt" fun

go语言笔记——map map 默认是无序的,不管是按照 key 还是按照 value 默认都不排序

示例 8.1 make_maps.go package main import "fmt" func main() { var mapLit map[string]int //var mapCreated map[string]float32 var mapAssigned map[string]int mapLit = map[string]int{"one": 1, "two": 2} mapCreated := make(map[strin

译文:Java8 Map Enhancement

此篇为Jooq官方博客的一篇译文,特此声明. Java 8 的好处: Map Enhancements 此次增强的 大部分API实际上是 新的Streams API的一部分.但是一些新的特性同样加入到 java.util.List 之中 并且最重要的是对 java.util.Map 的增强. 为了保证向后兼容, 所有被加入到Interface中的方法皆为默认方法.因此我们在使用过程中会有一些意外的小惊喜. compute() methods 过去,我们常获取一个map集合的view,在view上

5.map

Declare, initialize and iterate // Sample program to show how to declare, initialize and iterate // over a map. Shows how iterating over a map is random. package main import "fmt" // user defines a user in the program. type user struct { name st

Go structs、slices、maps

[Go structs.slices.maps] 1.定义时*在变量名后面,使用时*在变量名前面. 2.定义struct,type在前,struct关键字在后. 3.指针可以指定struct. 4.A struct literal denotes a newly allocated struct value by listing the values of its fields. You can list just a subset of fields by using the Name: sy

来自 Thoughtram 的 Angular 2 系列资料

Angular 2 已经正式 Release 了,Thoughtram 已经发布了一系列的文档,对 Angular 2 的各个方面进行深入的阐释和说明. 我计划逐渐将这个系列翻译出来,以便对大家学习 Angular 2 提供一些方便. Getting Started Building a Zippy component in Angular 2 Developing a Tabs component in Angular 2 Angular 2 Template Syntax demystifi

[转]50 Shades of Go: Traps, Gotchas, and Common Mistakes for New Golang Devs

http://devs.cloudimmunity.com/gotchas-and-common-mistakes-in-go-golang/ 50 Shades of Go: Traps, Gotchas, and Common Mistakes for New Golang Devs Go is a simple and fun language, but, like any other language, it has a few gotchas... Many of those gotc