2019-06-30 22:44:15 +02:00
|
|
|
struct User {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
2019-06-30 13:44:08 +02:00
|
|
|
struct A {
|
2019-06-30 22:44:15 +02:00
|
|
|
m map[string]int
|
|
|
|
users map[string]User
|
2019-06-30 13:44:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (a mut A) set(key string, val int) {
|
|
|
|
a.m[key] = val
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_map() {
|
|
|
|
mut m := map[string]int{}
|
|
|
|
m['hi'] = 80
|
|
|
|
assert m['hi'] == 80
|
2019-06-30 22:44:15 +02:00
|
|
|
////
|
|
|
|
mut users := map[string]User{}
|
|
|
|
users['1'] = User{'Peter'}
|
|
|
|
peter := users['1']
|
|
|
|
assert peter.name == 'Peter'
|
2019-06-30 13:44:08 +02:00
|
|
|
|
|
|
|
mut a := A{
|
2019-06-30 22:44:15 +02:00
|
|
|
m: map[string]int{}
|
|
|
|
users: map[string]User{}
|
2019-06-30 13:44:08 +02:00
|
|
|
}
|
2019-06-30 22:44:15 +02:00
|
|
|
a.users['Bob'] = User{'Bob'}
|
|
|
|
q := a.users['Bob']
|
|
|
|
assert q.name == 'Bob'
|
2019-06-30 13:44:08 +02:00
|
|
|
a.m['one'] = 1
|
|
|
|
a.set('two', 2)
|
|
|
|
assert a.m['one'] == 1
|
|
|
|
assert a.m['two'] == 2
|
|
|
|
}
|