From f9af59f4d1868248ab5ad82039cd8a37ee3c888e Mon Sep 17 00:00:00 2001 From: Archie Fox Date: Thu, 30 Jan 2025 19:32:45 +0300 Subject: [PATCH] add chapter 5 and refactor root path --- chapter4/hi/main.go | 2 +- chapter4/hi/main.go~ | 14 +++++++++++ chapter4/passfail/pass_fail.go | 2 +- chapter4/passfail/pass_fail.go~ | 23 +++++++++++++++++ chapter4/tocelsius/tocelsius.go | 2 +- chapter4/tocelsius/tocelsius.go~ | 17 +++++++++++++ chapter5/array/array.go | 42 ++++++++++++++++++++++++++++++++ chapter5/average/main.go | 13 ++++++++++ 8 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 chapter4/hi/main.go~ create mode 100644 chapter4/passfail/pass_fail.go~ create mode 100644 chapter4/tocelsius/tocelsius.go~ create mode 100644 chapter5/array/array.go create mode 100644 chapter5/average/main.go diff --git a/chapter4/hi/main.go b/chapter4/hi/main.go index 1db68d6..8c3e594 100644 --- a/chapter4/hi/main.go +++ b/chapter4/hi/main.go @@ -2,7 +2,7 @@ package main import ( "fmt" - "golang-book/head-first-go/chapter4/greeting" + "headfirstgo/headfirstgo/chapter4/greeting" ) func main() { diff --git a/chapter4/hi/main.go~ b/chapter4/hi/main.go~ new file mode 100644 index 0000000..8c3e594 --- /dev/null +++ b/chapter4/hi/main.go~ @@ -0,0 +1,14 @@ +package main + +import ( + "fmt" + "headfirstgo/headfirstgo/chapter4/greeting" +) + +func main() { + greeting.Hello() + greeting.Hi() + + slc := []string{"hello", "world"} + fmt.Printf("%s %T %T", slc[0], slc, true) +} diff --git a/chapter4/passfail/pass_fail.go b/chapter4/passfail/pass_fail.go index 6723978..16a8468 100644 --- a/chapter4/passfail/pass_fail.go +++ b/chapter4/passfail/pass_fail.go @@ -2,7 +2,7 @@ package main import ( "fmt" - "golang-book/head-first-go/chapter4/keyboard" + "headfirstgo/headfirstgo/chapter4/keyboard" "log" ) diff --git a/chapter4/passfail/pass_fail.go~ b/chapter4/passfail/pass_fail.go~ new file mode 100644 index 0000000..16a8468 --- /dev/null +++ b/chapter4/passfail/pass_fail.go~ @@ -0,0 +1,23 @@ +package main + +import ( + "fmt" + "headfirstgo/headfirstgo/chapter4/keyboard" + "log" +) + +func main() { + fmt.Print("Enter a grade: ") + grade, err := keyboard.GetFloat() + if err != nil { + log.Fatal(err) + } + + var status string + if grade >= 60 { + status = "passing" + } else { + status = "failing" + } + fmt.Println("A grade of", grade, "is", status) +} diff --git a/chapter4/tocelsius/tocelsius.go b/chapter4/tocelsius/tocelsius.go index deece7d..3ccca25 100644 --- a/chapter4/tocelsius/tocelsius.go +++ b/chapter4/tocelsius/tocelsius.go @@ -2,7 +2,7 @@ package main import ( "fmt" - "golang-book/head-first-go/chapter4/keyboard" + "headfirstgo/headfirstgo/chapter4/keyboard" "log" ) diff --git a/chapter4/tocelsius/tocelsius.go~ b/chapter4/tocelsius/tocelsius.go~ new file mode 100644 index 0000000..3ccca25 --- /dev/null +++ b/chapter4/tocelsius/tocelsius.go~ @@ -0,0 +1,17 @@ +package main + +import ( + "fmt" + "headfirstgo/headfirstgo/chapter4/keyboard" + "log" +) + +func main() { + fmt.Print("Enter a temperature in Farenheit: ") + farenheit, err := keyboard.GetFloat() + if err != nil { + log.Fatal(err) + } + celsius := (farenheit - 32) * 5 / 9 + fmt.Printf("%0.2f degrees Celsius\n", celsius) +} diff --git a/chapter5/array/array.go b/chapter5/array/array.go new file mode 100644 index 0000000..69be1d5 --- /dev/null +++ b/chapter5/array/array.go @@ -0,0 +1,42 @@ +package main + +import "fmt" + +func main() { + // 1 способ инициализации массива + // имя тип + var array [5]string + + // 2 способ - литерал + var arr2 = [3]int{0, 1, 2} + + // 3 способ + arr3 := [2]bool{false, true} + + fmt.Print(arr2, array, arr3, "\n") + + // доступ к элементу массива по индексу + fmt.Println(arr3[1]) + + // присваивание значения элементу массива + array[3] = "Hello Go!" + fmt.Println(array) + fmt.Printf("%#v\n", array) + + // перебор элементов массива в цикле for + for i := 0; i < 5; i++ { + fmt.Printf("%d %#v\n", i, array[i]) + } + + // длина массива + fmt.Println("Длина массива 'array' =", len(array)) + for i := 0; i < len(array); i++ { + fmt.Println(i, array[i]) + } + + fmt.Println() + // безопасный перебор массива + for idx, value := range array { + fmt.Println(idx, value) + } +} diff --git a/chapter5/average/main.go b/chapter5/average/main.go new file mode 100644 index 0000000..cbb8f39 --- /dev/null +++ b/chapter5/average/main.go @@ -0,0 +1,13 @@ +package main + +import "fmt" + +func main() { + numbers := [3]float64{71.8, 56.2, 89.5} + var sum float64 = 0 + for _, num := range numbers { + sum += num + } + + fmt.Println(sum) +}