A
A
Arthur2018-02-26 21:28:48
go
Arthur, 2018-02-26 21:28:48

How to display Cyrillic in the browser on GO?

Good afternoon, there is a code, an example from the Internet, that displays text from the address bar.
Let 's say
1. Going to example.com/test will display "Hello test"
2. Going to example.com/test will display "Hello %D1%82%D0%B5%D1%81%D1%82"
Google says to use "net /url", but when I try I get an error:

./index.go:16:35: url.PathUnescape undefined (type string has no field or method PathUnescape)

Full code:
package main

import (
    "net/http"
    "strings"
    "net/url"
)

func sayHello(w http.ResponseWriter, r *http.Request) {
    name := "World"
    url := strings.Trim(r.RequestURI, "/")
    if len(url) > 0 {
        name = strings.Split(url, "/")[0]
        name = strings.ToUpper(name[0:1]) + name[1:] // change first symbol to Uppercase
    }
    w.Write([]byte("Hello, " + url.PathUnescape(name) + "!"))
}

func main() {
    http.HandleFunc("/", sayHello)
    err := http.ListenAndServe(":9001", nil)
    if err != nil {
        panic(err.Error())
    }
}

Actually the question is, how is the conversion of% into a readable form implemented by standard methods? We really need Cyrillic urls.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Arthur, 2018-02-28
@ar2rsoft

The problem is in the url variable declared before calling url.QueryUnescape.

C
chupasaurus, 2018-02-26
@chupasaurus

Read RFC , Luke.

  1. Decode URI with QueryUnescape from net/url
  2. ?????
  3. PROFIT

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question