How to check if a variable in a string is blank [closed]

Want to achieve

I am building an app with Go. It is to create a ruby file from an excel file.

If there is a value in the xx, I want to put it in the data, and if it is blank, I want to skip the process. However, if I put nil as shown below, I get an error.

xx = xx + 19
if row[xx] ! = nil {
  data["IndustryId"] = row[xx];
  }
invalid operation: row[xx] != nil (mismatched types string and nil)

I hope you can help me.

Code

test.go

func main() {
  excel_file, err := excelize.OpenFile("./excel/data.xlsx")
  if err != nil {
    fmt.Println(err)
    return
  }

  seeds := make([]string, 0, 1000)
  seeds = append(seeds, "# Seed")
  seeds = append(seeds, "# " + time.Now().Format("RFC3339"))
  seeds = append(seeds, "")

  tpl := template.Must(template.ParseFiles(`test.tmpl`))

  rows, err := excel_file.GetRows("Test")
  for i, row := range rows {
    if i != 0 {

      xx := 2
      data := map[string]string{
        "Id": row[xx],
      }


      xx = xx + 19
      if row[xx] != nil {
        data["IndustryId"] = row[xx];
      }

      if err := tpl.Execute(&output, data); err != nil {
        fmt.Println(err)
        return
      }

      seeds = append(seeds, output.String())
    }
  }

  export_file("./seeds/import_test.rb", seeds)
}

Solution 1:

rows, err := excel_file.GetRows("Test")

Here's rows is of type [][]string. Now when you do:

for i, row := range rows { ... }

row is of []string and now if you index it, you'll get a string.

The zero value of a string is "" (empty string) and not nil. So, please compare it with "" instead of nil. Here:

row[xx] != ""