Skip to content

Instantly share code, notes, and snippets.

@sugilog
Created January 3, 2015 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sugilog/a5fef0ddddf60f27c781 to your computer and use it in GitHub Desktop.
Save sugilog/a5fef0ddddf60f27c781 to your computer and use it in GitHub Desktop.
mapの操作
package main
import "fmt"
func main() {
fmt.Println( "withNilMap" )
withNilMap()
fmt.Println( "withMakeMap" )
withMakeMap()
fmt.Println( "withEmptyInitMap" )
withEmptyInitMap()
fmt.Println( "withInitMap" )
withInitMap()
}
func withNilMap() {
var m map[string]int
fmt.Println( m )
fmt.Println( m[ "hoge" ] )
// m[ "hoge" ] = 1
fmt.Println( m == nil )
}
func withMakeMap() {
m := make( map[string]int )
fmt.Println( m )
fmt.Println( m[ "hoge" ] )
}
func withEmptyInitMap() {
m := map[string]int{}
fmt.Println( m )
fmt.Println( m[ "hoge" ] )
}
func withInitMap() {
m := map[string]int{
"hoge": 1,
"fuga": 2,
}
fmt.Println( m )
fmt.Println( m[ "hoge" ] )
v1, b1 := m[ "hoge" ]
fmt.Println( v1, b1 )
v2, b2 := m[ "abc" ]
fmt.Println( v2, b2 )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment