Created
January 3, 2015 14:57
-
-
Save sugilog/a5fef0ddddf60f27c781 to your computer and use it in GitHub Desktop.
mapの操作
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" | |
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