package main
import (
"fmt"
"log"
)
func main() {
code := map[string]interface{}{
"test": "test",
}
// извлекаем interface{} из мапы внутри которого строка
ifaceStr := code["test"]
// извлекаем строку из interface{}
str, ok := ifaceStr.(string)
if !ok {
log.Fatal("it's not a string")
}
fmt.Println(str)
}
r.Handler("GET", "/", fServer)
r.Handler("GET", "/*filepath", fServer)
main redeclared in this block {строка 8 столбец 6 }
previous declaration at ./hello.go:18:6
path := `c:\Program files\some program\program.exe`
// аналогично вызову в консоли start "c:\Program files\some data"
cmd := exec.Command("start", path)
......
// аналогично вызову в консоли "c:\Program files\some program\program.exe"
cmd := exec.Command(path)
......
// аналогично вызову в консоли some_prog.exe -flag1 value1 -flag2 value2
cmd := exec.Command("some_prog.exe", "-flag1", "value1", "-flag2", "value2")
......
map[string][]func(string, interface{})
package main
import "fmt"
func main() {
var some = float64(5) / float64(3)
fmt.Println(some)
fmt.Printf("%T", some)
}
package main
import (
"fmt"
"regexp"
)
func main() {
tokens := tokenize("something, something1, othersomething")
fmt.Println(tokens)
fmt.Println("some", tokens.containsWord("some"))
fmt.Println("thing", tokens.containsWord("thing"))
fmt.Println("something", tokens.containsWord("something"))
}
var re = regexp.MustCompile(`[\s,.;"']+`)
type TokenizedString map[string]struct{}
func tokenize(str string) TokenizedString {
list := re.Split(str, -1)
tokens := make(map[string]struct{}, len(list))
for _, token := range list {
tokens[token] = struct{}{}
}
return tokens
}
func (t TokenizedString) containsWord(word string) bool {
_, ok := t[word]
return ok
}
go mod init somename
вместо somename принято ставить путь к репозиторию вашего проекта, но можно и просто имя. type CustomError struct {
Text string
HttpCode int
}
func (c *CustomError) Error() string {
return c.Text
}
func main() {
err := doSomeStuff()
if err != nil {
customErr := &CustomError{}
if errors.As(err, &customErr) {
fmt.Println(customErr.Text, " http code:", customErr.HttpCode)
}
}
}
func doSomeStuff() error {
return &CustomError{
Text: "please provide a password",
HttpCode: 401,
}
}
func WriteJson(d structs.Bassketball) error {
f, err := os.OpenFile("basketball.json", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
rawJSON, _ := json.Marshal(d)
_, err = f.Write(rawJSON)
if err != nil {
return err
}
return nil
}