Search code examples
linuxwebservergo

Convert port to :port of type string in Golang


How do you convert a port inputted as a int by the user to a string of type ":port" (ie, it should have a ':' in front of it and should be converted to string). The output has to be feed to http.ListenAndServe().


Solution

  • if err := http.ListenAndServe(fmt.Sprintf(":%d", port), handler); err != nil {
            log.Fatal(err)
    }