7 votes

Fortnightly Programming Q&A Thread

General Programming Q&A thread! Ask any questions about programming, answer the questions of other users, or post suggestions for future threads.

Don't forget to format your code using the triple backticks or tildes:

Here is my schema:

```sql
CREATE TABLE article_to_warehouse (
  article_id   INTEGER
, warehouse_id INTEGER
)
;
```

How do I add a `UNIQUE` constraint?

2 comments

  1. [2]
    crdpa
    Link
    I'm making a server in Go for my new page. Something simple. There will be three sections. Index, blog and resume. Here is the code: server.go package main import ( "fmt"...

    I'm making a server in Go for my new page. Something simple.

    There will be three sections. Index, blog and resume. Here is the code:

    • server.go
    package main
    
    import (
    	"fmt"
    	"github.com/crdpa/crdpa.net/routes"
    	"log"
    	"net/http"
    )
    
    func main() {
    	routes.RouteHandler()
    
    	fmt.Printf("Starting server at port 8000\n")
    	if err := http.ListenAndServe(":8000", nil); err != nil {
    		log.Fatal(err)
    	}
    }
    
    • routes.go
    package routes
    
    import (
    	//    "github.com/crdpa/crdpa.net/"
    	"html/template"
    	"net/http"
    )
    
    var temp = template.Must(template.ParseGlob("templates/*.html"))
    
    func index(w http.ResponseWriter, r *http.Request) {
    	temp.ExecuteTemplate(w, "Index", nil)
    }
    
    func blog(w http.ResponseWriter, r *http.Request) {
    	temp.ExecuteTemplate(w, "Blog", nil)
    }
    
    func resume(w http.ResponseWriter, r *http.Request) {
    	temp.ExecuteTemplate(w, "Resume", nil)
    }
    
    func RouteHandler() {
    	http.HandleFunc("/", index)
    	http.HandleFunc("/blog", blog)
    	http.HandleFunc("/resume", resume)
    }
    

    I want the site to have two languages. Portuguese and English.

    So the urls would be /en/blog /en/resume /en/ for english and without /en for portuguese.

    It's just a matter of creating three more functions and doing:

    http.HandleFunc("en/", index_en)
    http.HandleFunc("en/blog", blog_en)
    http.HandleFunc("en/resume", resume_en)
    

    or is there a better way?

    And those functions "index", "blog" and "resume" are too similar, it just changes one word. Is there a better way of coding this?

    2 votes
    1. pvik
      (edited )
      Link Parent
      It depends on how much you want to abstract the translations away. An easier way to do this for a simpler application would be to use a query parameter like lang ( yoursite.com/blog?lang=en or...

      It depends on how much you want to abstract the translations away.

      An easier way to do this for a simpler application would be to use a query parameter like lang ( yoursite.com/blog?lang=en or yoursite.com/blog)

      Your hanndler function can parse for the lang query param, and default to Portuguese (if not present).

      If you want to have to define the language as part of your routes, then I would suggest using something like chi, which would let you define your route like

          r.Route("/{lang}", func(r chi.Router) {
            r.Get("/blog", blog)
            r.Get("/resume", resume) 
          })
      

      Slightly off-topic, This is also a good post that lets you incorporate some internationalization in your pages (similar to res/values/strings.xml in an Android application) using the message and language packages.

      2 votes