Basic operation of golang

Build an http service

package main
 
import (
	"io"
	"log"
	"net/http"
)
 
func HelloServer(w http.ResponseWriter, r *http.Request) {
	io.WriteString(w, "hello ,this is from HelloServer func ")
}
 
func main() {
	http.HandleFunc("/hello", HelloServer)
	log.Fatal(http.ListenAndServe(":12345", nil))
}

Build an https service

func main() {
	http.HandleFunc("/handleString", handler)
	err := http.ListenAndServeTLS(":8081", "cret/ca.crt",
		"cret/server.pem", nil)
	if err != nil {
		print(err.Error())
	}
}

Escaping and parsing of json string

//转义,返回的是[]byte,需要string
lang, err := json.Marshal(stringList)f
if err == nil {

}

	
//解析
	var resBool []bool
	err = json.Unmarshal(body, &resBool)
	if err != nil {
		fmt.Println(err)
	}

Determine whether the key exists in the map

if _, ok := map[key]; ok {

Determine whether the element exists in the slice

func Find(slice []string, val string) (int, bool) {
    for i, item := range slice {
        if item == val {
            return i, true
        }
    }
    return -1, false
}

Guess you like

Origin blog.csdn.net/weixin_42094764/article/details/113614948