go版本:1.13
go run -mod=vendor main.go
go get -mod=vendor github.com/gin-gonic/gin
go get -u C:\Users\Administrator\Desktop\gin-master
1、类型转换 integer to ASCII string
1.int转换为字符串:Itoa()
// Itoa(): int -> string
println("a" + strconv.Itoa(32)) // a32
2.string转换为int:Atoi()
// Atoi(): string -> int
i,_ := strconv.Atoi("3")
println(3 + i) // 6
// Atoi()转换失败
i,err := strconv.Atoi("a")
if err != nil {
println("converted failed")
}
2、 row查询
deviceRows, dbErr1 := models.DB.Model(&models.Device{}).Select("device_number").Where("region_id in (?)", regionids).Rows()
if dbErr1 != nil {
utils.JsonResponse(c, 400, "查询失败: "+dbErr1.Error(), nil)
return
}
var deviceIds []string
for deviceRows.Next() {
var deviceId string
err := deviceRows.Scan(&deviceId)
if err != nil {
utils.JsonResponse(c, 400, "数据库查询赋值变量失败: "+err.Error(), nil)
return
}
deviceIds = append(deviceIds, deviceId)
}
结构体查询
var devices []models.Device
err = models.DB.Where("id in (?) ", deviceids).Find(&devices).Error
if err != nil {
utils.JsonResponse(c, 400, "查询设备失败:"+err.Error(), "")
return
}
var devicenum string
var devicenumbers []string
for i := 0; i < len(devices); i++ {
devicenum = devices[i].DeviceNumber
devicenumbers = append(devicenumbers, devicenum)
}
go run main.go