布尔值意外地从true变为false

huangapple go评论70阅读模式
英文:

boolean values set to true unintentionally changing to false

问题

在我的golang应用程序中,我根据表单输入将一些全局变量设置为true,然后发现在后续的函数中它们变为了false。问题是,如何在golang中声明和设置布尔值的正确方式?

var withKetchup bool
var withMustard bool

func orderProcess(w http.ResponseWriter, r *http.Request){

     r.ParseForm()
     withKetchup := r.FormValue("withKetchup")  //设置为true(在表单的js选择中)
     withMustard := r.FormValue("withMustard")  //设置为true
     
      //省略的代码
}

func prepareOrder(){

      //省略的代码
     fmt.Println(withKetchup, withMustard) //尽管最初设置为true,但两者都为false
     if withKetchup == true && withMustard == true{

     }else {

     }


}

正确的方式是在orderProcess函数中,将withKetchupwithMustard的值直接赋给全局变量,而不是使用:=来重新声明并创建新的局部变量。这样,全局变量的值就会被正确地设置为true。修改后的代码如下:

var withKetchup bool
var withMustard bool

func orderProcess(w http.ResponseWriter, r *http.Request){

     r.ParseForm()
     withKetchup = r.FormValue("withKetchup")  //设置为true(在表单的js选择中)
     withMustard = r.FormValue("withMustard")  //设置为true
     
      //省略的代码
}

func prepareOrder(){

      //省略的代码
     fmt.Println(withKetchup, withMustard) //现在会正确地输出true
     if withKetchup == true && withMustard == true{

     }else {

     }


}

这样修改后,全局变量的值将会被正确地保留,并且在prepareOrder函数中可以正确地使用。

英文:

In my golang application, I'm setting some global variables to true based on form input, and then discovering that they have changed to false when used in a subsequent function. Question, what is the proper way to declare and set Boolean values in golang?

var withKetchup bool
var withMustard bool

func orderProcess(w http.ResponseWriter, r *http.Request){

     r.ParseForm()
     withKetchup := r.FormValue("withKetchup")  //set to true (in form js form selection)
     withMustard := r.FormValue("withMustard")  //set to true
     
      code omitted ///
}

func prepareOrder(){

      code omitted//
     fmt.Println(withKetchup, withMustard) //both are false even though initially set to true
     if withKetchup == true && withMustard == true{

     }else {

     }


}

答案1

得分: 7

代码

withKetchup := r.FormValue("withKetchup")

声明并设置一个本地变量类型为字符串使用短变量声明要设置全局布尔变量将语句转换为赋值语句去掉 ":"同时通过将表单值与""进行比较计算一个布尔值

withKetchup = r.FormValue("withKetchup") == "true"

由于服务器并发执行处理程序不安全使用全局变量我建议将这些值作为参数传递给prepareOrder函数

func orderProcess(w http.ResponseWriter, r *http.Request){

     r.ParseForm()
     withKetchup := r.FormValue("withKetchup") == "true"
     withMustard := r.FormValue("withMustard") == "true"
     prepareOrder(withKetchup, withMustard)
}

func prepareOrder(withKetchup, withMustard bool){
     if withKetchup == true && withMustard == true{

     }else {

     }
}
英文:

The code

 withKetchup := r.FormValue("withKetchup") 

declares and sets a local variable of type string using a short variable declaration. To set the global bool variable, convert the statement to an assignment by removing the ":". Also, compute a bool value by comparing the form value with "":

 withKetchup = r.FormValue("withKetchup") == "true"

Because the server executes handlers concurrently, it's not safe to use a global variable like this. I suggest passing the values as arguments to prepareOrder:

func orderProcess(w http.ResponseWriter, r *http.Request){

     r.ParseForm()
     withKetchup := r.FormValue("withKetchup") == "true"
     withMustard := r.FormValue("withMustard") == "true"
     prepareOrder(withKetchup, withMustard)
}

func prepareOrder(withKetchup, withMustard bool){
     if withKetchup == true && withMustard == true{

     }else {

     }
}

huangapple
  • 本文由 发表于 2015年3月26日 10:29:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/29270083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定