Hi,

I have applied similar scenario to the function below but am not getting 
the Ip address and the port number. Below is the function

var addr string

func init(){

var conf Config
if _, err := toml.Decode("config.toml", &conf); err != nil {
        // handle error
    }
    addr = net.JoinHostPort(conf.Addr, conf.Port)
    fmt.Println(addr)  // This printing empty
 
}


func main() {
    
    server := NewServer()

    go func() {

        if err := 
server.ListenAndServeTLS("/home/cumucore/diam/server.crt", 
"/home/cumucore/diam/server.key"); err != nil && err != 
http.ErrServerClosed {
            logger.Fatalf("Could not listen on %s: %v\n", addr, err)

            if err := server.ListenAndServe(); err != nil {
                logger.Fatalf("Could not listen on %s: %v\n", addr, err)
            }

        }
    }()
}


// Create a new server
func NewServer() *http.Server {

    return &http.Server{
        Addr:         addr,
        Handler:      diam.NewRouter(),
        ErrorLog:     logger,
        ReadTimeout:  5 * time.Second,
        WriteTimeout: 10 * time.Second,
        IdleTimeout:  15 * time.Second,
    }
}

Ther config.toml file contain the key values as
Addr = "192.168.9.186"
Port = "8000"

What might be the problem in this case


On Sunday, September 29, 2019 at 5:18:55 PM UTC+3, Andrew Pillar wrote:
>
> Use net.JoinHostPort to concatenate the values you have in the struct 
> and pass the to http.Server struct. 
>
>   if _, err := toml.Decode("config.toml", &conf); err != nil { 
>     // handle error 
>   } 
>
>   addr, err := net.JoinHostPort(conf.Address, conf.PORT) 
>
>   if err != nil { 
>     // handle error 
>   } 
>
>   src := &http.Server{ 
>     Addr: addr, 
>   } 
>
> Be sure to set explicit struct tags on your destination struct that 
> will be used for unmarshalling the toml. This way the decoder will know 
> which struct fields to populate. 
>
>   type Config struct { 
>     PORT    string `toml:"port"` 
>     Address string `toml:"address"` 
>   } 
>
> This will only be necessary though if you want the fields to map 
> differently depending on their name. 
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/9756a565-79c7-4eda-96f3-172e3ec6a908%40googlegroups.com.

Reply via email to