(基础篇)go常用使用场景一

一、循环

循环两种方式,这两个实现的效果是一样的,都是循环根据数组长度下标做判断:

var categoryListSt []string
for i,value := range categoryList{
    if i == len(categoryList)-1{
    categoryListSt = append(categoryListSt, "'"+value+"'")
    }else{
    categoryListSt = append(categoryListSt, "'"+value+"'"+",")
    }
}
for i := 0; i < len(categoryList); i++ {
        if i == len(categoryList)-1{
            categoryListSt = append(categoryListSt, "'"+categoryList[i]+"'")
        }else{
            categoryListSt = append(categoryListSt, "'"+categoryList[i]+"'"+",")
        }
    }

二、处理字符串:

//上面就是string的list
//字符串的拼接
result := strings.Join(categoryListSt, "")
​
//字符串可以和[]byte切片互相转换
b := []byte{'H', 'e', 

你可能感兴趣的:(go语言学习笔记,golang,go)