第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何檢查字符串中的變量是否為空白

如何檢查字符串中的變量是否為空白

Go
人到中年有點(diǎn)甜 2022-10-24 16:20:08
想要達(dá)到我正在用 Go 構(gòu)建一個(gè)應(yīng)用程序。它是從 excel 文件創(chuàng)建一個(gè) ruby 文件。如果xx中有值,我想把它放在數(shù)據(jù)中,如果它是空白的,我想跳過這個(gè)過程。但是,如果我如下所示輸入 nil,則會(huì)出現(xiàn)錯(cuò)誤。xx = xx + 19if row[xx] ! = nil {  data["IndustryId"] = row[xx];  }invalid operation: row[xx] != nil (mismatched types string and nil)我希望你能幫助我。代碼測(cè)試.gofunc 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)}
查看完整描述

2 回答

?
明月笑刀無(wú)情

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超4個(gè)贊

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

rows是類型[][]string?,F(xiàn)在當(dāng)你這樣做時(shí):

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

row是的[]string,現(xiàn)在如果你索引它,你會(huì)得到一個(gè)字符串。

字符串的零值是""(空字符串) 而不是nil。因此,請(qǐng)將其與""而不是進(jìn)行比較nil。這里:

row[xx] != ""


查看完整回答
反對(duì) 回復(fù) 2022-10-24
?
波斯汪

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超4個(gè)贊

您應(yīng)該 Trim row[xx] 以確保該行不只包含空格并將其與“”而不是 nil 進(jìn)行比較。


import strings 

....

strings.TrimSpace(row[xx]) != "" 


查看完整回答
反對(duì) 回復(fù) 2022-10-24
  • 2 回答
  • 0 關(guān)注
  • 121 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)