This commit is contained in:
parent
8d79ac08a3
commit
cf4ee051bb
@ -3,7 +3,15 @@ when:
|
||||
branch: main
|
||||
|
||||
steps:
|
||||
build:
|
||||
go-test:
|
||||
image: golang:1.24
|
||||
environment:
|
||||
CGO_ENABLED: 0
|
||||
GOOS: linux
|
||||
GOARCH: amd64
|
||||
commands:
|
||||
- go test ./...
|
||||
go-build:
|
||||
image: golang:1.24
|
||||
environment:
|
||||
CGO_ENABLED: 0
|
||||
|
||||
10
main.go
10
main.go
@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -21,6 +22,15 @@ func main() {
|
||||
mux.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
|
||||
json.NewEncoder(w).Encode(map[string]string{"version": version})
|
||||
})
|
||||
mux.HandleFunc("/sum", func(w http.ResponseWriter, r *http.Request) {
|
||||
a, _ := strconv.Atoi(r.URL.Query().Get("a"))
|
||||
b, _ := strconv.Atoi(r.URL.Query().Get("b"))
|
||||
json.NewEncoder(w).Encode(map[string]int{"result": sum(a, b)})
|
||||
})
|
||||
|
||||
http.ListenAndServe(":8080", mux)
|
||||
}
|
||||
|
||||
func sum(a, b int) int {
|
||||
return a + b
|
||||
}
|
||||
|
||||
22
main_test.go
Normal file
22
main_test.go
Normal file
@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_sum(t *testing.T) {
|
||||
tests := []struct {
|
||||
a, b int
|
||||
want int
|
||||
}{
|
||||
{1, 2, 3},
|
||||
{-1, 1, 0},
|
||||
{0, 0, 0},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
if got := sum(test.a, test.b); got != test.want {
|
||||
t.Errorf("sum(%d, %d) = %d, want %d", test.a, test.b, got, test.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user