This question already has an answer here:
I have the following code:
package main
import (
"net/http"
"log"
"net"
"fmt"
"os"
"encoding/json"
)
const configName string = "config.json"
type Config struct {
UDPServerAddress string
HTTPServerAddress string
}
var config Config
func UDProutine (query string, ch chan<- string) {
log.Fatal(config.UDPServerAddress)
}
func main () {
file,_ := os.Open(configName)
defer file.Close()
decoder := json.NewDecoder(file)
config := Config{}
err := decoder.Decode(&config)
if err != nil {
fmt.Println("error",err)
}
log.Fatal(config.UDPServerAddress)
}
And in my config.json
{
"UDPServerAddress":"127.0.0.1:54",
"HTTPServerAddress":"127.0.0.1:8082"
}
My question is, why does it log the config data correctly inside main
but its empty value when logged inside UDPRoutine
</div>