Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

map keys method

package main

import (
	"fmt"
)

type hash map[string]int

func (h hash) keys() []string {
	keys := make([]string, 0, len(h))
	for k := range h {
		keys = append(keys, k)
	}
	return keys
}

func main() {
	data := hash{
		"foo": 3,
		"bar": 17,
		"adi": 10,
	}
	fmt.Println(data)
	ks := data.keys()
	fmt.Println(ks)
}
map[adi:10 bar:17 foo:3]
[foo bar adi]