0
我有用golang編寫的proto3/grpc函數。有一個if語句寫在開關中,當值爲0時,不會看到int32的值爲0.我之前打印該值,它是0,但if語句仍然運行。在我的代碼下面我有評論輸出。我知道一個int值爲0的零。如果我爲lname放置一個值,那麼它們的fname就像它應該那樣工作。任何幫助讚賞。這裏是我的輸出:Golang if語句沒有看到0爲int32
map[fname: lname: email: id:0]
0
id = $1
這裏是我的代碼:
func (s *server) GetUsers(ctx context.Context, in *userspb.User) (*userspb.Users, error) {
flds := make(map[string]interface{})
flds["id"] = in.Id // 0
flds["fname"] = in.Fname // "" (empty)
flds["lname"] = in.Lname // "" (empty)
flds["email"] = in.Email // "" (empty)
fmt.Println(flds) //map[lname: email: id:0 fname:]
var where bytes.Buffer
n := 0
for _, v := range flds {
switch v.(type) {
case string:
if v != "" {
n++
}
case int, int32, int64:
if v != 0 {
n++
}
}
}
calledvariables := make([]interface{}, 0)
i := 1
for k, v := range flds {
switch v.(type) {
case string:
if v != "" {
if i != 1 {
where.WriteString(" AND ")
}
ist := strconv.Itoa(i)
where.WriteString(k + " = $" + ist)
calledvariables = append(calledvariables, v)
i++
}
case int, int32, int64, uint32, uint64:
/////// THIS IF STATMENT IS THE ISSUE the (v is printing the value of 0 and it's in the if statement)
if v != 0 {
fmt.Println(v) // 0
if i != 1 {
where.WriteString(" AND ")
}
ist := strconv.Itoa(i)
where.WriteString(k + " = $" + ist)
calledvariables = append(calledvariables, v)
i++
}
}
}
fmt.Println(where.String()) // id = $1
...
每天學習一些東西。謝謝 –
我們今天都學到了這一點,這只是一個瘋狂的猜測,我在發佈之前在操場上確認過。不是我所期望的,但它是有道理的。文字的類型並不總是很清楚。 – Adrian