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
}
type Forks []struct {
ForkID string `json:"fork_id"`
Income float64 `json:"income"`
Sport string `json:"sport"`
...............
AliveSec int `json:"alive_sec"`
ValuingData ValuingData `json:"valuing_data"`
}
type Fork struct {
ForkID string `json:"fork_id"`
Income float64 `json:"income"`
Sport string `json:"sport"`
...............
AliveSec int `json:"alive_sec"`
ValuingData ValuingData `json:"valuing_data"`
}
type Forks []Fork
func checkBet(body []byte, v *Fork) {
......
func main() {
fn := some
fmt.Println(fn())
fn = other
fmt.Println(fn())
}
func some() int {
return 1
}
func other() int {
return 2
}
func makeRequest() error {
req, err := http.NewRequest(http.MethodGet, "https://habr.com", nil)
if err != nil {
return err
}
req.Header.Add("User-Agent", "mySuperTestApp v1.0")
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return fmt.Errorf("wrong status code: %d", res.StatusCode)
}
body, err := io.ReadAll(res.Body)
if err != nil {
return err
}
fmt.Println(string(body))
return nil
}
func (h *Human) Speak() {
}
func Speak(h *Human) {
}
package main
import (
"encoding/base64"
"fmt"
)
func main() {
srcString := "Привет, тут есть юникод"
fmt.Println("Исходный текст:", srcString)
codedStr := base64.StdEncoding.EncodeToString([]byte(srcString))
fmt.Println("После кодирования:", string(codedStr))
resBytes, err := base64.StdEncoding.DecodeString(codedStr)
if err != nil {
fmt.Println("Ошибка декодирования:", err)
return
}
resString := string(resBytes)
fmt.Println("После декодирования обратно:", resString)
}
var serv = &http.Server {
Addr: "127.0.0.1:600",
ReadTimeout: 15 * time.Second,
WriteTimeout: 15 * time.Second,
Handler: mux, // <---- здесь
}
func allRout(w http.ResponseWriter, r *http.Request) {
http.FileServer(http.Dir("F:/Projects/www/domen.com/")).ServeHTTP(w, r)
}