more
This commit is contained in:
parent
76394d4ea4
commit
0f5553f15d
18
17-maps.go
Normal file
18
17-maps.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
x := make(map[string]int)
|
||||||
|
x["this"] = 7
|
||||||
|
x["that"] = 13
|
||||||
|
fmt.Println(x)
|
||||||
|
fmt.Println(x["this"])
|
||||||
|
fmt.Println(x["missing"])
|
||||||
|
fmt.Println(len(x))
|
||||||
|
delete(x, "that")
|
||||||
|
fmt.Println(x["that"])
|
||||||
|
fmt.Println(len(x))
|
||||||
|
_, present := x["that"]
|
||||||
|
fmt.Println(present)
|
||||||
|
}
|
16
18-guard.go
Normal file
16
18-guard.go
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
x := make(map[string]string)
|
||||||
|
x["here"] = "yep"
|
||||||
|
if name, ok := x["not here"]; ok {
|
||||||
|
fmt.Println(name)
|
||||||
|
} else {
|
||||||
|
fmt.Println("missing")
|
||||||
|
}
|
||||||
|
if name, ok := x["here"]; ok {
|
||||||
|
fmt.Println(name)
|
||||||
|
}
|
||||||
|
}
|
29
19-nesting.go
Normal file
29
19-nesting.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
elements := map[string]map[string]string{
|
||||||
|
"He": map[string]string{
|
||||||
|
"name":"Helium",
|
||||||
|
"state":"gas",
|
||||||
|
},
|
||||||
|
"Li": map[string]string{
|
||||||
|
"name":"Lithium",
|
||||||
|
"state":"solid",
|
||||||
|
},
|
||||||
|
"Be": map[string]string{
|
||||||
|
"name":"Beryllium",
|
||||||
|
"state":"solid",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(elements)
|
||||||
|
fmt.Println()
|
||||||
|
|
||||||
|
li := elements["Li"]
|
||||||
|
fmt.Println(li)
|
||||||
|
fmt.Println()
|
||||||
|
|
||||||
|
fmt.Println(li["name"], li["state"])
|
||||||
|
}
|
18
20-functions.go
Normal file
18
20-functions.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func avg(vals []float64) float64 {
|
||||||
|
total := 0.0
|
||||||
|
for _, val := range vals {
|
||||||
|
total += val
|
||||||
|
}
|
||||||
|
return total / float64(len(vals))
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
input := []float64{98,93,77,82,83}
|
||||||
|
fmt.Println(input)
|
||||||
|
output := avg(input)
|
||||||
|
fmt.Println(output)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user