package main import ( "fmt" "github.com/mitchellh/mapstructure" ) type myStruct struct { X, Y int } func (p myStruct) method() { fmt.Println(p.X) fmt.Println(p.Y) } func (p *myStruct) methodPtr() { fmt.Println(p.X) fmt.Println(p.Y) } type Point struct { X int Y int } func main() { /* http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello world") }) http.ListenAndServe(":9000", nil) */ //test2() // test3() // test4() // test5() // test6() // test7() test8() } func test1() { var name string = "Dmitry" var age int = 23 var c = fmt.Sprintf("My name is %s and ma age is %d", name, age) fmt.Println(c) } func test2() { for i := 0; i < 10; i++ { fmt.Println(i) } } func test3() { s1 := myStruct{ X: 1, Y: 2, } s2 := myStruct{X: 123, Y: 23434} //ptr := &s2 s1.method() s2.methodPtr() } func test4() { var a [2]string a[0] = "hello" a[1] = "world" numbers := [...]int{1, 2, 3} fmt.Println(a) fmt.Println(numbers) // Слайсы. Не имеют фиксированной длины letters := []string{"a", "b", "c"} letters[1] = "sadfasd" letters = append(letters, "new element", "werwe") // Пустой слайс createSlice := make([]string, 3) fmt.Println(letters) fmt.Println(len(createSlice)) fmt.Println(cap(createSlice)) } func test5() { animalArr := [4]string{ "dog", "cat", "giraffe", "elephant", } /* animalSlic := []string{ "dog", "cat", "giraffe", "elephant", } */ a := animalArr[0:2] fmt.Println(a) b := animalArr[1:3] fmt.Println(b) b[0] = "123" fmt.Println(a) fmt.Println(animalArr) } func test6() { arr := []string{"a", "b", "c"} for i, l := range arr { fmt.Println(i, l) } for _, l := range arr { fmt.Println(l) } } func test7() { pointMap := map[string]Point{} otherMap := map[string]Point{ "b": {X: 23, Y: 12}, } otherPointMap := make(map[int]Point) pointMap["a"] = Point{ X: 1, Y: 2, } fmt.Println(pointMap) fmt.Println(pointMap["a"]) otherPointMap[1] = Point{X: 3, Y: 7} fmt.Println(otherPointMap[1]) fmt.Println(otherMap["b"]) key := 2 value, ok := otherPointMap[key] if ok { fmt.Printf("key=%d exist in map", key) fmt.Println(value) } else { fmt.Printf("key=%d does exist in map", key) fmt.Println(value) } } func test8() { pointsMap := map[string]int{ "X": 1, "Y": 2, } p1 := Point{} mapstructure.Decode(pointsMap, &p1) fmt.Println(p1) for k, v := range pointsMap { fmt.Println(k) fmt.Println(v) } }