英文:
How to check if a variable in a string is blank
问题
想要实现的功能
我正在使用Go构建一个应用程序。
它的功能是从Excel文件中创建一个Ruby文件。
如果xx中有一个值,我想将其放入数据中,如果为空,则跳过该过程。
然而,如果我像下面所示放置nil,我会得到一个错误。
xx = xx + 19
if row[xx] != nil {
data["IndustryId"] = row[xx];
}
invalid operation: row[xx] != nil (mismatched types string and nil)
希望你能帮助我。
代码
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)
}
以上是你要翻译的内容。
英文:
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)
}
答案1
得分: 4
rows, err := excel_file.GetRows("Test")
这里的rows
是[][]string
类型。现在当你这样做:
for i, row := range rows { ... }
row
是[]string
类型,如果对它进行索引,你将得到一个字符串。
字符串的零值是""
(空字符串),而不是nil
。所以,请将其与""
进行比较,而不是nil
。在这里:
row[xx] != ""
英文:
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] != ""
答案2
得分: -1
你应该使用Trim函数来确保row[xx]不仅仅包含空格,并将其与""进行比较,而不是与nil进行比较。
import strings
....
strings.TrimSpace(row[xx]) != ""
英文:
You should Trim row[xx] to ensure that the row does not contain only space and compare it with "" not with nil.
import strings
....
strings.TrimSpace(row[xx]) != ""
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论