[TOC] ## 打印中文字符 > rune相当于go的char ``` s := "我哈哈啊!" for _, v := range []rune(s) { fmt.Printf("%c \n",v) //每次打印一个字符 } ``` ## 递归+迭代实现无限级分类 > [参考网址] ``` type Cate struct { Id int `json:"id"` Name string `json:"name"` Pid int `json:"pid"` } type cateTree struct { Id int `json:"id"` Name string `json:"name"` Pid int `json:"pid"` Son []cateTree } func main() { allCate := []Cate{ {1, "a", 0}, {2, "b", 0}, {3, "c", 0}, {4, "aa", 1}, {5, "bb", 2}, {6, "cc", 3}, {7, "aaa", 4}, {8, "bbb", 5}, {9, "哈哈哈", 6}, {10, "ccc", 7}, } arr := superCategoryTree(allCate, 0) b, _ := json.Marshal(arr) fmt.Println("json:", string(b)) //必须使用string(b) 进行转义 } //@allCate 结构体数组 //@pid int 顶级id func superCategoryTree(allCate []Cate, pid int) []cateTree { var arr []cateTree for _, v := range allCate { if pid == v.Pid { ctree := cateTree{} ctree.Id = v.Id ctree.Pid = v.Pid ctree.Name = v.Name sonCate := superCategoryTree(allCate, v.Id) ctree.Son = sonCate arr = append(arr, ctree) } } return arr } ``` ## go 并发调用 python 或 shell > [参考代码](https://github.com/r00tjimmy/codeBox/blob/master/golang_basic/goroutine_exec.go) ``` var quit= make(chan int) func runComm() { cmd := exec.Command("date", "+%s.%N") out, err := cmd.CombinedOutput() if err != nil { fmt.Println(err) } fmt.Println(string(out)) time.Sleep(3*time.Second) quit <- 0 } func main() { runtime.GOMAXPROCS(2) go runComm() go runComm() for i := 0; i < 2; i++ { <- quit } } ``` ## 守护其他进程的代码 ``` func main() { file, e := os.Create("angel.txt") if err != nil { os.Exit(1) } defer lf.Close() // 日志 l := log.New(lf, "", os.O_APPEND) for { cmd := exec.Command("/usr/local/bin/node", "/*****.js") err := cmd.Start() if err != nil { l.Printf("%s 启动命令失败", time.Now().Format("2006-01-02 15:04:05"), err) time.Sleep(time.Second * 5) continue } l.Printf("%s 进程启动", time.Now().Format("2006-01-02 15:04:05"), err) err = cmd.Wait() l.Printf("%s 进程退出", time.Now().Format("2006-01-02 15:04:05"), err) time.Sleep(time.Second * 1) } } ``` ## 执行系统命令 如`curl ` > window 无法运行系统自带命令 ``` bytes, _ := UseOutput("curl", "-help") func UseOutput(name string, arg ...string) ([]byte, error) { return exec.Command(name, arg...).Output() } ``` ## map 转 struct ``` func SetField(obj interface{}, name string, value interface{}) error { structValue := reflect.ValueOf(obj).Elem() structFieldValue := structValue.FieldByName(name) if !structFieldValue.IsValid() { return fmt.Errorf("No such field: %s in obj", name) } if !structFieldValue.CanSet() { return fmt.Errorf("Cannot set %s field value", name) } structFieldType := structFieldValue.Type() val := reflect.ValueOf(value) if structFieldType != val.Type() { return errors.New("Provided value type didn't match obj field type") } structFieldValue.Set(val) return nil } func MapToStruct(m map[string]interface{},result interface{}) error { for k, v := range m { err := SetField(result, k, v) if err != nil { return err } } return nil } func main() { type MyStruct struct { Name string Age int64 } myData := make(map[string]interface{}) myData["Name"] = "Tony" myData["Age"] = int64(23) result := &MyStruct{} if err := MapToStruct(myData,result);err != nil { fmt.Println(err) } fmt.Println(result.Name) } ``` ## 中文所在字符串位置 ``` func Utf8Index(str, substr string) int { index := strings.Index(str, substr) if index < 0{ return -1 } return utf8.RuneCountInString(str[:index]) } ```