package main
import (
"fmt"
"net/http"
"os"
"strconv"
"github.com/go-chi/chi"
)
var tests []string
func main() {
if len(os.Args[:]) > 1 {
tests = append(tests, "test")
fmt.Println(strconv.Itoa(len(tests)) + " tests")
os.Exit(0)
}
r := chi.NewRouter()
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
tests = append(tests, "test")
fmt.Println(strconv.Itoa(len(tests)) + " tests")
})
http.ListenAndServe(":8086", r)
}
When i access the program from localhost:8086 it works okay and i can see len(tests)
increment when i refresh.
But when i access the program with ./test arg
command, len(tests)
increments to 1 but then gets lost, so next time it says 1 and never goes any higher.
I thought that these two methods should also share the same tests
value, but when i write to one, the other can't read it etc.
Thanks.