I'm trying to add <title>Go</title>
to my code example:
package main
import (
"fmt"
"net/http"
"os"
)
func favicon(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "favicon.ico")
}
func sayhelloName(w http.ResponseWriter, r *http.Request) {
hostname, _ := os.Hostname()
fmt.Fprintf(w, "
System info:
Hostname [pod name]: %s", hostname)
fmt.Fprintf(w, "
Current URL: %s
", r.Host)
}
func main() {
http.HandleFunc("/favicon.ico", favicon)
http.HandleFunc("/", sayhelloName)
http.ListenAndServe(":80", nil)
}
I tried to add like: fmt.Fprintf(w, "<title>Go</title>")
. It works but make mess with strings next to. I wouldn't like to use html template only to add title to page. Is there any ways to add title in one string?
not used html template. just used only fmt.Fprintf.
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func favicon(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "favicon.ico")
}
func sayhelloName(w http.ResponseWriter, r *http.Request) {
hostname, _ := os.Hostname()
fmt.Fprintf(w, "<html>")
fmt.Fprintf(w, "<head>")
fmt.Fprintf(w, "<title>%s</title>", "Go")
fmt.Fprintf(w, "</head>")
fmt.Fprintf(w, "<body>")
fmt.Fprintf(w, "<h1>System info: Hostname [pod name]: %s", hostname)
fmt.Fprintf(w, "<h1>Current URL: %s", r.Host)
fmt.Fprintf(w, "</body>")
fmt.Fprintf(w, "</html>")
}
func main() {
http.HandleFunc("/favicon.ico", favicon)
http.HandleFunc("/", sayhelloName)
log.Fatal(http.ListenAndServe(":8080", nil))
}