my/go/l19/l2.go

73 lines
1.3 KiB
Go
Raw Normal View History

2026-06-25 16:40:34 +03:00
package main
import (
"fmt"
"net/http"
"encoding/json"
"time"
"bytes"
"io"
2026-06-26 19:56:18 +03:00
"strings"
2026-06-25 16:40:34 +03:00
)
type UserStruct struct{
Age int `json:"age"`
Name string `json:"name"`
Email string `json:"email"`
2026-06-26 19:56:18 +03:00
Alive bool `json:"alive"`
}
func polz() (int, string, string, bool) {
fmt.Printf("Your age? ")
var age int
fmt.Scan(&age)
fmt.Printf("Your name? ")
var name string
fmt.Scan(&name)
fmt.Printf("Your email? ")
var email string
fmt.Scan(&email)
fmt.Printf("Your alive?(y/n) ")
var live string
fmt.Scan(&live)
LowerLive := strings.ToLower(live)
var alive bool
if LowerLive == "y" {
alive = true
} else if LowerLive == "n" {
alive = false
}
return age, name, email, alive
2026-06-25 16:40:34 +03:00
}
func main() {
2026-06-26 19:56:18 +03:00
age, name, email, alive := polz()
2026-06-25 16:40:34 +03:00
user := UserStruct{
2026-06-26 19:56:18 +03:00
Age: age,
Name: name,
Email: email,
Alive: alive,
2026-06-25 16:40:34 +03:00
}
JsonData, err := json.Marshal(user)
if err != nil {
panic(err)
}
2026-06-25 16:50:07 +03:00
client := &http.Client{
2026-06-25 16:40:34 +03:00
Timeout: 5 * time.Second,
2026-06-25 16:50:07 +03:00
}
2026-06-25 16:40:34 +03:00
req, err := http.NewRequest("POST", "http://127.0.0.1:7777/user", bytes.NewBuffer(JsonData))
if err != nil {
panic(err)
}
2026-06-25 16:50:07 +03:00
resp, err := client.Do(req)
if err != nil {
panic(err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Printf("status code %d\n", resp.StatusCode)
fmt.Println(string(body))
2026-06-26 19:56:18 +03:00
}