add go std lib project

This commit is contained in:
2025-07-08 03:46:12 +08:00
parent e5dd781966
commit f8702ac1b9
2 changed files with 51 additions and 0 deletions

3
go-stdlib/go.mod Normal file
View File

@ -0,0 +1,3 @@
module money-transfer
go 1.24.2

48
go-stdlib/main.go Normal file
View File

@ -0,0 +1,48 @@
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
type Transfer struct {
Amount int `json:"amount"`
Sender int `json:"sender"`
Receiver int `json:"receiver"`
}
var accounts = map[int]int{
1: 1000,
2: 1000,
}
func main() {
fmt.Println("Starting server on port 8080")
http.HandleFunc("/transfer", transfer)
err := http.ListenAndServe(":8081", nil)
if err != nil {
fmt.Println("Error starting server:", err)
}
}
func transfer(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var transfer Transfer
err = json.Unmarshal(body, &transfer)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Println("Transfer received:", transfer)
w.WriteHeader(1000)
w.Write([]byte("Transfer successful"))
}