Created
October 18, 2022 21:48
-
-
Save Elshaman/4ad9cfa656b850b3ccec36d1b69f0c90 to your computer and use it in GitHub Desktop.
Golang: find a key in a map; key passed via command line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
o run main.go 204 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
) | |
var persons map[string]string | |
func find(key string){ | |
if val, ok := persons[key]; ok { | |
fmt.Println("Hi,",val) | |
}else{ | |
fmt.Println("Sorry, not found") | |
} | |
} | |
func main(){ | |
persons = make(map[string]string) | |
persons["305"] = "Sue" | |
persons["204"] = "Bob" | |
persons["631"] = "Jake" | |
persons["073"] = "Tracy" | |
key := os.Args[1] | |
find(key) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment