package main
import (
"fmt"
)
type HandlersMap map[string]func(string) string
func (hm HandlersMap) RegisterHandler(path string, handler func(string) string) {
hm[path] = handler
}
func main() {
m := make(HandlersMap, 0)
m.RegisterHandler("1", myFunc1)
m.RegisterHandler("2", myFunc2)
handler1 := m["1"]
handler2 := m["2"]
fmt.Println(handler1("handler 1 output"))
fmt.Println(handler2("handler 2 output"))
}
func myFunc1(s string) string {
return fmt.Sprintf("MyFunc1: %s", s)
}
func myFunc2(s string) string {
return fmt.Sprintf("MyFunc2: %s", s)
}
$ go run main.go
MyFunc1: handler 1 output
MyFunc2: handler 2 output
func handleRequest(conn net.Conn) {
defer conn.Close()
buf := make([]byte, 1024)
b := bufio.NewReader(conn)
for {
line, err := b.ReadBytes('\n')
if err != nil { // for example EOF
break
}
buf = append(buf, line...)
}
fmt.Print(string(buf))
}
func handleRequest(conn net.Conn) {
message := make([]byte, 1024)
_, error := conn.Read(message)
if error != nil {
fmt.Println(error)
os.Exit(1)
}
fmt.Println(string(message))
conn.Close()
}
https://domain.name/data.json?searchtext=Запрос+На+Кириллице
package main
import (
"fmt"
"net/url"
)
func main() {
q := "Запрос+На+Кириллице"
u := "https://domain.name/data.json"
fmt.Println(u + "?searchtext=" + url.QueryEscape(q))
}
$ go run main.go
https://domain.name/data.json?searchtext=%D0%97%D0%B0%D0%BF%D1%80%D0%BE%D1%81%2B%D0%9D%D0%B0%2B%D0%9A%D0%B8%D1%80%D0%B8%D0%BB%D0%BB%D0%B8%D1%86%D0%B5
go get golang.org/x/net/idna
package main
import (
"fmt"
"golang.org/x/net/idna"
)
var p *idna.Profile
func main() {
p = idna.New()
fmt.Println(p.ToASCII("россия.рф"))
}
$ go run main.go
xn--h1alffa9f.xn--p1ai <nil>
type MyType ext_api.Data
func (m *MyType) PrintDataEx() {...}
type MyStruct struct {
*ext_api.Data
}
func (m *MyStruct) Method() {...}
m := &MyStruct{...}
m.Method()
package main
import (
"fmt"
"html"
)
func main() {
const s = `"Fran & Freddie's Diner" <tasty@example.com>`
fmt.Println(html.EscapeString(s))
}
"Fran & Freddie's Diner" <tasty@example.com>
import (
"net/http"
"net/url"
)
func main() {
urlStr := "http://..."
request, err := http.NewRequest("GET", urlStr, nil)
proxyURL, err := url.Parse("http://username:password@host:port")
transport := &http.Transport{
Proxy: http.ProxyURL(proxyURL),
}
client := &http.Client{
Transport: transport,
}
response, err := client.Do(request)
}