Init commit

This commit is contained in:
Archie Fox
2024-12-02 12:32:31 +03:00
parent 61976c9a29
commit 2e7b8722ca
6 changed files with 119 additions and 0 deletions

BIN
chapter1/hello Executable file

Binary file not shown.

7
chapter1/hello.go Normal file
View File

@@ -0,0 +1,7 @@
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
}

53
chapter2/guess/guess.go Normal file
View File

@@ -0,0 +1,53 @@
// guess - игра про отгадывание числа
package main
import (
"fmt"
"math/rand"
// "time"
"bufio"
"log"
"os"
"strconv"
"strings"
)
func main() {
/* seconds := time.Now().Unix()
rand.Seed(seconds) */ // is deprecated
target := rand.Intn(100) + 1
fmt.Println("Я загадал число от 1 до 100. Отгадай его: ")
// fmt.Println(target)
reader := bufio.NewReader(os.Stdin) // новый буфер для чтения с клавы
success := false
for guesses := 0; guesses < 10; guesses++ {
fmt.Println("У вас есть", 10-guesses, "попыток.")
fmt.Print("Введите свое число: ")
input, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
input = strings.TrimSpace(input) // удаление новой строки
guess, err := strconv.Atoi(input) // конвертация в int
if err != nil {
log.Fatal(err)
}
if guess < target {
fmt.Println("Ваше число меньше загаданного")
} else if guess > target {
fmt.Println("Ваше число больше загаданного")
} else if guess == target {
success = true
fmt.Println("Вы угадали число!!!")
break
}
}
if !success {
fmt.Println("Вы проиграли! Загаданное число", target)
}
}

View File

@@ -0,0 +1,33 @@
// pass_fail сообщает сдал ли пользователь экзамен
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
func main() {
fmt.Print("Enter a grade: ")
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
input = strings.TrimSpace(input)
grade, err := strconv.ParseFloat(input, 64)
if err != nil {
log.Fatal(err)
}
var status string
if grade >= 60 {
status = "passing"
} else if grade < 60 {
status = "failing"
}
fmt.Println("A grade of", grade, "is", status)
}

View File

@@ -0,0 +1,13 @@
package main
import (
"fmt"
"strings"
)
func main() {
broken := "G# r#cks!"
replacer := strings.NewReplacer("#", "o")
fixed := replacer.Replace(broken)
fmt.Println(fixed)
}

13
chapter2/time.go Normal file
View File

@@ -0,0 +1,13 @@
package main
import (
"fmt"
"time"
)
func main() {
var now time.Time = time.Now()
var year int = now.Year()
fmt.Println(year)
fmt.Println(now)
}