英文:
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
函数中,将withKetchup
和withMustard
的值直接赋给全局变量,而不是使用:=
来重新声明并创建新的局部变量。这样,全局变量的值就会被正确地设置为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 {
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论