👊
Go tutorial for Pythoner
  • Introduction
  • Install and Run Go
  • The structure of Go
    • First time you meet him
    • Functions
    • Return multiple results
    • Logic Control
    • Break loop and enumerate
    • List (Array or Slice)
    • Dict (Map)
    • Struct
    • Class (Methods)
    • Packages
  • Features of Go
  • Go modules
  • Web Server
    • Serving static files
    • A simple webserver
  • Go for Android
  • Where to go next
Powered by GitBook
On this page
  • The map, exactly, is Python's dictionary.
  • Golang version
  • Python version
  • What if we want a type of dict-list combination?

Was this helpful?

  1. The structure of Go

Dict (Map)

The map, exactly, is Python's dictionary.

Golang version

package main

import "fmt"

func iterate_map_A(A map[string]string) {
    for key, value := range A {
        fmt.Println(key, value)
    }
}

func iterate_map_B(A map[string]int) {
    for key, value := range A {
        fmt.Println(key, value)
    }
}

func main() {
    mapA := make(map[string]string) // make a map where the key is string, the value is also a string
    mapA["anyone"] = "lacks of patience" 
    mapA["that's"] = "bad" 
    iterate_map_A(mapA)

    fmt.Println("")

    mapB := map[string]int{"me": 100, "you": 0}
    iterate_map_B(mapB)
}

Python version

def iterate_dict(a_dict):
    for index, value in a_dict.items():
        print(index, value)

dict_A = dict({'anyone': 'lacks of patience', "that's": "bad"})
iterate_dict(dict_A)

print('\n')

dict_B = dict({"me": 100, "you": 0})
iterate_dict(dict_B)

What if we want a type of dict-list combination?

package main

import (
    "fmt"
)

var dict_list = make(map[int][]string)

func main() {
    dict_list[1] = []string{"Everyone", "one"}
    dict_list[2] = []string{"can", "be"}
    dict_list[3] = []string{"its", "own", "god."}

    fmt.Println(dict_list)

    for key, list := range dict_list {
        for _, word := range list {
            if key <= 3 {
                fmt.Printf("%s ", word)
            }
        }
    }
}
PreviousList (Array or Slice)NextStruct

Last updated 4 years ago

Was this helpful?