cannot initialize 2 variables with 1 values

What’s wrong with this m, err := os.Truncate(path,0) if err != nil { log.Printf("Failed to truncate: %v", err) } if I do above I am getting an error like this cannot initialize 2 variables with 1 values while this works without an issue if err := os.Truncate(path, 0); err != nil { log.Printf("Failed to truncate:… Read More cannot initialize 2 variables with 1 values

scanning a string separated by ":" in golang

I need to string scan 23.584950:58.353603:an address, str:a place, name into : type Origin struct { Lat float64 `json:"lat" binding:"required"` Lng float64 `json:"lng" binding:"required"` Address string `json:"address" binding:"required"` Name string `json:"name" binding:"required"` } My code: package main import "fmt" type Origin struct { Lat float64 `json:"lat" binding:"required"` Lng float64 `json:"lng" binding:"required"` Address string `json:"address" binding:"required"`… Read More scanning a string separated by ":" in golang

How to run a specific function each time someone enters my website in go?

I’m using the following code: func main() { http.Handle("/", http.FileServer(http.Dir("./web-files"))) http.HandleFunc("/auth/verify", verify) http.HandleFunc("/auth/login", login) http.HandleFunc("/auth/signup", signup) http.ListenAndServe(":8080", nil) } I’m new to go and what I want to do is that every time someone enters my webpage a function named updateCookies runs. I’ve tried to use http.HandleFunc("/", updateCookies) but it didn’t work cause I have… Read More How to run a specific function each time someone enters my website in go?

How to load both .env file and the os environment variables in golang

err := godotenv.Load(".env") if err != nil { panic(err.Error()) } shell := os.Getenv("SHELL") fmt.Println(shell) I set the SHELL=/bin/zsh in my .env file but it seems the os first look for the given key in the os environment variable list and then It checks the .env file . is there a way to separate these two… Read More How to load both .env file and the os environment variables in golang

Ineffective Assignment to Field when trying to update a Struct in Go

I’m getting the linting error ineffective assignment to field Player.Level (SA4005)go-staticcheck when I try to use a struct method LevelUp to update the struct’s value Player.Level: func main() { player := Player{ Name: "Tom", Level: 0, } player.LevelUp() fmt.Printf("Player level %d\n", player.Level) } type Player struct { Name string Level int } func (p Player)… Read More Ineffective Assignment to Field when trying to update a Struct in Go

ResponseWriter interface in Go net/http package

I have two questions about the ResponseWriter interface in net/http package. Here is my code package main import ( "fmt" "net/http" "os" ) type handler struct{} func (h *handler) ServeHTTP(w http.ResponseWriter, req *http.Request) { fmt.Fprintf(w, "Method: %v\n", req.Method) fmt.Fprintf(w, "URL: %v\n", req.URL) fmt.Fprintf(w, "Protocol: %v\n", req.Proto) fmt.Fprintf(os.Stdout, "Header: %v\n", w.Header()) } func main() { http.ListenAndServe(":8080",… Read More ResponseWriter interface in Go net/http package