How to handle null data in SQL for struct in golang

type account struct {
   id   int   name string   age  sql.NullInt64   amt  sql.NullFloat64}

func testReadNullvalue(db *sqlx.DB) {
   rows, _ := db.Query("select id, name, age , amount from account")
   for rows.Next() {
      var foo account      rows.Scan(&foo.id, &foo.name, &foo.age, &foo.amt)

      if foo.amt.Valid==true {
         fmt.Println("value: %v",foo.amt.Float64 )
      }else {
         fmt.Println("data is null, print with default value ",foo.amt.Float64 )

      }
      fmt.Println("%+v", foo)
   }

}

Comments

Popular posts from this blog

Fixing the DeepSpeed Import Error While Fine-Tuning the Qwen Model

Amazon Linux 2023 - User data configuration for launch templates to connect to the EKS cluster

How to create ISM policy and rotate logs in opensearch