Go-开发中遇到的小tips

时间类型转换

在sqldb/*.go中的时间类型为:

1 type name struct {
2     CreateDate *time.Time `sql:"CreateDate"`
3 }

以上对应数据库中的字段类型为DateTime

在doctor/*.go中的时间类型为:

type name struct {
    CreateDate *types.Time `json:"createDate"`
}

以上时间来源为小程序输入(应该是time类型 没确认),也可在后端传入,如下:

now := types.Time(time.Now())
argument.CreateDate = &now

两种时间类型之间的转换如下:

createDate := types.Time(*sqldb.CreateDate)
doctor.CreateDate = &createDate

createDate := time.Time(*doctor.CreateDate)
sqldb.CreateDate = &createDate

类型转换

Map转String

var answerMap map[uint64]uint64
......//中间操作
answerJson,_ := json.Marshal(answerMap)
answerString := string(answerJson)

数据增删查改

若找到则更新 没找到则新增

先进行update的操作,然后:

if err != nil {
    if sqlAccess.IsNoRow(err) {
        _, err = sqlAccess.InsertSelective(dbEntity)
    } else {
    return ......//某种error
    }
}

循环

List类型循环

需要用到index和value

for index, value := range answerList {
......//需要的操作
}

定时任务

位置在nusiness/patient/business.go

if now.Weekday() == 1 && now.Hour() == 2 {
   // 每周一凌晨2:00生成上周的营养干预分析
   sTime := now.AddDate(0, 0, -1)
   instance.data.CreateNutritionInterventionReport(&sTime)
}

数据结构相关

数据库字段类型是JSON

在sqldb中对应为String,在doctor对应为interface,也可以是具体的一个接口比如:

......
    DietPlan *DietPlanRecord   `json:"dietPlan"`
}
//在doctor/dietRecord.ext.go
type DietPlanRecord struct {
   Grain     uint64    `json:"grain" note:"这一类完成该目标的次数"`
   Vegetable  uint64        `json:"vegetable"`
   Fruit     uint64        `json:"fruit"`
   Oil       uint64    `json:"oil"`
   Sweet     uint64    `json:"sweet"`
   Drink     uint64    `json:"drink"`
   Pickle    uint64    `json:"pickle"`
   Milk      uint64    `json:"milk"`
   Fish        uint64    `json:"fish"`
   Bean      uint64    `json:"bean"`
   Mushroom   uint64    `json:"mushroom"`
   Water       uint64    `json:"water"`
   Place       uint64    `json:"place"`
}
//copyto: string to inferface
record := *s.DietPlan
if len(record) > 0 {
   json.Unmarshal([]byte(record), &target.DietPlan)
}
//copyfrom: inferface to string
if source.DietPlan != nil {
   recordData, err := json.Marshal(source.DietPlan)
   if err == nil {
      record := string(recordData)
      s.DietPlan = &record
   }
}

AnotherDataStructureBase

A数据结构里写了B-Based,在设置A数据结构的值的时候怎么设置B的部分?

type SportDietRecordDetaFilterEx struct{
    SportDietRecordDataFilter
    PatientID uint64 `json:"patientID"`
}

//在实例化时
placeFilter := &doctor.SportDietRecordDataFilterEx{
   PatientID:       filter.PatientID,
   SportDietRecordDataFilter: doctor.SportDietRecordDataFilter{
      HappenStartDate: (*types.Time)(startTime),
      HappenEndDate: (*types.Time)(endTime),
   },
}

String方法

//这是一些 strings 中的函数例子。注意他们都是包中的函数,不是字符串对象自身的方法,这意味着我们需要考虑在调用时传递字符作为第一个参数进行传递。
strings.Contains("test", "es")//是否包含:true
strings.Count("test", "t")//包含字符数 :2
strings.HasPrefix("test", "te")//是否包含前缀:true
strings.HasSuffix("test", "st")//是否包含后缀:true
strings.Index("test", "d")//字符存在的位置,不存在返回-1
strings.Join([]string{"a", "b"}, "-")//拼接字符:a-b
strings.Repeat("abc", 5)//复制字符几次:aaaaa
strings.Replace("foo", "o", "0", -1)//替换所有存在的字符 f00
strings.Replace("foo", "o", "0", 0)//替换存在的字符一次 f0o
strings.Split("a-b-c-d-e", "-")//指定字符分隔字符串:[a b c d e]
strings.ToLower("TEST")                        
strings.ToUpper("test")
len("hello")
"hello"[1]
strings.Fields("hello")//字符串变数组
strings.TrimSpace("   hell o  ")
}

 

posted @ 2022-10-21 09:37  Tracydzf  阅读(19)  评论(0编辑  收藏  举报