A simple and beautiful blog
struct
1234567891011121314151617181920212223242526272829303132333435
package maintype Power struct { Ts time.Time Current float32 Voltage int Phase float32}func main() { var taosUri = "root:taosdata@tcp(localhost:6030)/power" taos, err := sql.Open("taosSql", taosUri) if err != nil { fmt.Println("failed to connect TDengine, err:", err) return } sql := "select * from d1001" rows, err := taos.Query(sql) defer rows.Close() for rows.Next() { power := Power{} s := reflect.ValueOf(&power).Elem() numCols := s.NumField() columns := make([]interface{}, numCols) for i := 0; i < numCols; i++ { field := s.Field(i) columns[i] = field.Addr().Interface() } err := rows.Scan(columns...) if err != nil { fmt.Println(err) } log.Printf("采集时间:%v, 当前值:%v, 电压:%d", power.Ts, power.Current, power.Voltage) }}
Seraching...