0
0

More than 3 years have passed since last update.

スライスの中にマップ

Last updated at Posted at 2021-01-12

メモとして残します


package main

import (
    "fmt"
)

func main() {
    // ① スライス var slice []string
    // ② マップ  var map1 map[string]string

    // スライスの中にmapを作るには、①と②を組み合わせるだけ
    var slice []map[string]string
    slice = append(slice, map[string]string{"apple": "red", "banana": "yellow"})
    slice = append(slice, map[string]string{"kiwi": " green"})

    fmt.Println(slice)              // [map[apple:red banana:yellow] map[kiwi: green]]
    fmt.Println(slice[0])           // map[apple:red banana:yellow]
    fmt.Println(slice[1])           // map[kiwi: green]
    fmt.Println(slice[0]["apple"])  // red
    fmt.Println(slice[0]["banana"]) // yellow
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0