add chapter 4
This commit is contained in:
18
chapter4/const/const.go
Normal file
18
chapter4/const/const.go
Normal file
@@ -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)
|
||||
}
|
||||
11
chapter4/const/dates/dates.go
Normal file
11
chapter4/const/dates/dates.go
Normal file
@@ -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)
|
||||
}
|
||||
12
chapter4/const/planner/main.go
Normal file
12
chapter4/const/planner/main.go
Normal file
@@ -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")
|
||||
}
|
||||
Reference in New Issue
Block a user