Double return for server response

Trying to build simple web app that works with multiple languages, but I noticed the output is printed twice in my statement:

func index(w http.ResponseWriter, r *http.Request) {
		fmt.Println(lang.Value)
		fmt.Println(cfg.Database.Username)
}

The output I get is:

D:\Development\GO\i18n>go run hasan/web
Listening to port: 8090
en
admin
en
admin

And my full code is:

// main.go
package main

import (
	"fmt"
	"net/http"
	"os"
	"text/template"

	"gopkg.in/yaml.v2"
)

// Config ...
type Config struct {
	Server struct {
		Port string `yaml:"port"`
		Host string `yaml:"host"`
	} `yaml:"server"`
	Database struct {
		Username string `yaml:"user"`
		Password string `yaml:"pass"`
	} `yaml:"database"`
}

func processError(err error) {
	fmt.Println(err)
	os.Exit(2)
}

func readFile(cfg *Config, lang string) {
	f, err := os.Open(fmt.Sprint("configuration/", lang, ".yml"))
	if err != nil {
		processError(err)
	}
	defer f.Close()

	decoder := yaml.NewDecoder(f)
	err = decoder.Decode(cfg)
	if err != nil {
		processError(err)
	}
}

func index(w http.ResponseWriter, r *http.Request) {
	var tmpl *template.Template
	var cfg Config
	lang, err := r.Cookie("language")
	if err != nil {
		readFile(&cfg, "en")
		tmpl, _ = template.New("").ParseFiles(fmt.Sprint("templates/en/page-en.html"), "base.html")
	} else {
		readFile(&cfg, lang.Value)
		fmt.Println(lang.Value)
		fmt.Println(cfg.Database.Username)
		tmpl, _ = template.New("").ParseFiles(fmt.Sprint("templates/", lang.Value, "/page-", lang.Value, ".html"), "base.html")
	}

	tmpl.ExecuteTemplate(w, "base", nil)
}

func main() {
	port := "8090"
	http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./public"))))
	http.HandleFunc("/", index)
	fmt.Println("Listening to port:", port)
	error := http.ListenAndServe(":"+port, nil)
	fmt.Println(error)
}

And templates are:

// base.html
{{define "base"}}
<!DOCTYPE html>
<html lang="en">
    <link rel="shortcut icon" href="favicon.ico" />
    <script>
    function setLanguage(lang) {
        document.cookie = "language="+lang;
        location.reload();
    }
    </script>
<body>

    <a href="#" onclick="setLanguage('en')">English</a> 
    <a href="#" onclick="setLanguage('ar')">عربي</a>
    <br/>

    {{template "content" .}}

    <br/>
    footer...

</body>
</html>
{{end}}

// templates/page-en.html
{{define "content"}}
Welcome
{{end}}

// templates/page-ar.html
{{define "content"}}
أهلا و سهلا
{{end}}

The configuration files are:

// configuration/en.yml
# Server configurations
server:
  host: "localhost"
  port: 8000

# Database credentials
database:
  user: "admin"
  pass: "password"

//  configuration/ar.yml
# تعريف الخادم
server:
  host: "localhost"
  port: 8000

# كلمة الس لقاعدة البيانات
database:
  user: "مدير"
  pass: "كلمة سر"

The go.mod file is:

module hasan/web

go 1.16

require gopkg.in/yaml.v2 v2.4.0

Add a label to recognize which variable is printed, say

A possible cause could be that the browser request your page twice because of the favicon. At the first sight requesting the favicon and the page itself invoke the same handler.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.