diff --git a/chapter4/const/const.go b/chapter4/const/const.go new file mode 100644 index 0000000..1b0fb77 --- /dev/null +++ b/chapter4/const/const.go @@ -0,0 +1,18 @@ +package main + +import "fmt" + +// объявление константы с типом +const TriangleSides int = 3 + +// объявление константы без типа +const SquareSides = 4 + +/* константам переназначить значение невозможно +const PentagonSides = 5 +PentagonSides = 6 // error +*/ + +func main() { + fmt.Println(TriangleSides, SquareSides) +} diff --git a/chapter4/const/dates/dates.go b/chapter4/const/dates/dates.go new file mode 100644 index 0000000..65c8898 --- /dev/null +++ b/chapter4/const/dates/dates.go @@ -0,0 +1,11 @@ +package dates + +const DaysInWeek = 7 + +func WeeksToDays(weeks int) int { + return weeks * DaysInWeek +} + +func DaysToWeek(days int) float64 { + return float64(days) / float64(DaysInWeek) +} diff --git a/chapter4/const/planner/main.go b/chapter4/const/planner/main.go new file mode 100644 index 0000000..e281513 --- /dev/null +++ b/chapter4/const/planner/main.go @@ -0,0 +1,12 @@ +package main + +import ( + "fmt" + "golang-book/head-first-go/chapter4/const/dates" +) + +func main() { + days := 3 + fmt.Println("Your appointment is", days, "days") + fmt.Println("with a follow-up in", days+dates.DaysInWeek, "days") +} diff --git a/chapter4/greeting/greeting.go b/chapter4/greeting/greeting.go new file mode 100644 index 0000000..1c8b889 --- /dev/null +++ b/chapter4/greeting/greeting.go @@ -0,0 +1,11 @@ +package greeting + +import "fmt" + +func Hello() { + fmt.Println("Hello!") +} + +func Hi() { + fmt.Println("Hi!") +} diff --git a/chapter4/hi/main.go b/chapter4/hi/main.go new file mode 100644 index 0000000..1db68d6 --- /dev/null +++ b/chapter4/hi/main.go @@ -0,0 +1,14 @@ +package main + +import ( + "fmt" + "golang-book/head-first-go/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/keyboard/keyboard.go b/chapter4/keyboard/keyboard.go new file mode 100644 index 0000000..3eed530 --- /dev/null +++ b/chapter4/keyboard/keyboard.go @@ -0,0 +1,23 @@ +package keyboard + +import ( + "bufio" + "os" + "strconv" + "strings" +) + +func GetFloat() (float64, error) { + reader := bufio.NewReader(os.Stdin) + input, err := reader.ReadString('\n') + if err != nil { + return 0, err + } + + input = strings.TrimSpace(input) + number, err := strconv.ParseFloat(input, 64) + if err != nil { + return 0, err + } + return number, nil +} diff --git a/chapter4/passfail/pass_fail.go b/chapter4/passfail/pass_fail.go new file mode 100644 index 0000000..6723978 --- /dev/null +++ b/chapter4/passfail/pass_fail.go @@ -0,0 +1,23 @@ +package main + +import ( + "fmt" + "golang-book/head-first-go/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 new file mode 100644 index 0000000..deece7d --- /dev/null +++ b/chapter4/tocelsius/tocelsius.go @@ -0,0 +1,17 @@ +package main + +import ( + "fmt" + "golang-book/head-first-go/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) +}