protohackers/cmd/unusualdatabase/main.go

42 lines
662 B
Go
Raw Normal View History

package main
import (
"fmt"
"log"
"net"
"strings"
"protohackers/pkg/conn"
)
var (
db map[string]string
)
func init() {
db = make(map[string]string)
db["version"] = "0.1.1"
}
func main() {
err := conn.StartSimpleUDP(handler, 1024)
if err != nil {
log.Fatalln(err)
}
}
func handler(c *net.UDPConn, addr *net.UDPAddr, data []byte) {
if strings.Contains(string(data), "version=") {
return
} else if strings.Contains(string(data), "=") {
sp := strings.SplitN(string(data), "=", 2)
db[sp[0]] = sp[1]
} else {
out := fmt.Sprintf("%s=", string(data))
if v, ok := db[string(data)]; ok {
out += v
}
c.WriteTo([]byte(out), addr)
}
}