英文:
In go, how do I make global variables
问题
包 main
import (
"fmt"
"bufio"
"os"
)
var inputX, inputY string
func main() {
fmt.Print("LOADED!\n")
fmt.Print("在这里插入 y 的值:")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
inputY = scanner.Text()
inputXfunc()
}
func inputXfunc() {
fmt.Print("在这里插入 x 的值:")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
inputX = scanner.Text()
slope()
}
func slope() {
fmt.Println(inputY / inputX)
}
每当我运行这个程序时,它会显示 inputX 和 inputY 未定义。我该如何使该程序使用在所有函数中都可访问的变量?我只想做的是将 inputY 除以 inputX,然后打印出结果。
英文:
package main
import (
"fmt"
"bufio"
"os"
)
func main() {
fmt.Print("LOADED!\n")
fmt.Print("insert y value here: ")
inputY := bufio.NewScanner(os.Stdin)
inputY.Scan()
inputXfunc()
}
func inputXfunc() {
fmt.Print("insert x value here: ")
inputX := bufio.NewScanner(os.Stdin)
inputX.Scan()
slope()
}
func slope() {
fmt.Println(inputX.Text())
}
Whenever I run this program, it says, that inputX and inputY are unidentified. How do I make this program use variables that are accessible to all of the functions? All I want to do is devide inputY by inputX then print out the result
答案1
得分: 7
我只是将我的评论作为答案...不过我建议不要这样做,你可以在包范围内声明变量。代码如下:
package main
import (
"fmt"
"bufio"
"os"
)
var inputX io.Scanner
func main() {
fmt.Print("LOADED!\n")
fmt.Print("insert y value here: ")
inputY := bufio.NewScanner(os.Stdin)
inputY.Scan()
inputXfunc()
}
func inputXfunc() {
fmt.Print("insert x value here: ")
inputX = bufio.NewScanner(os.Stdin) // 在这里去掉赋值初始化的简写
inputX.Scan()
slope()
}
func slope() {
fmt.Println(inputX.Text())
}
然而,更好的选择是将你的方法定义更改为接受参数,并根据需要将值传递给它们。代码如下:
func slope(inputX bufio.Scanner) {
fmt.Println(inputX.Text())
}
slope(myInputWhichIsOfTypeIOScanner)
英文:
I'm just putting my comment as an answer... I would recommend against this however you could just declare the variable at package scope. It would look like this;
package main
import (
"fmt"
"bufio"
"os"
)
var inputX io.Scanner
func main() {
fmt.Print("LOADED!\n")
fmt.Print("insert y value here: ")
inputY := bufio.NewScanner(os.Stdin)
inputY.Scan()
inputXfunc()
}
func inputXfunc() {
fmt.Print("insert x value here: ")
inputX = bufio.NewScanner(os.Stdin) // get rid of assignment initilize short hand here
inputX.Scan()
slope()
}
func slope() {
fmt.Println(inputX.Text())
}
However a better choice would be to change your method definitions to accept arguments and pass the values into them as needed. This would like like so;
func slope(inputX bufio.Scanner) {
fmt.Println(inputX.Text())
}
slope(myInputWhichIsOfTypeIOScanner)
答案2
得分: 3
你可以创建一个init()
函数,并在main.go
中使用godotenv
等包来设置操作系统的环境变量:
global.go
文件
package global
import (
"log"
"os"
"strconv"
"github.com/joho/godotenv"
)
var (
SERVER_HOST string
SERVER_PORT int
)
func InitConfig() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
SERVER_HOST = os.Getenv("SERVER_HOST")
SERVER_PORT, _ = strconv.Atoi(os.Getenv("SERVER_PORT"))
}
main.go
文件
package main
import (
G "path/to/config"
)
func init() {
G.InitConfig()
}
func main() {
G.Init()
}
你仍然需要在其他包中导入G
包来使用这些变量,但我认为处理Go(或任何其他语言)中的全局变量的最佳方法是利用环境变量。
英文:
You can create an init()
function and make use of it in the main.go
by using package like godotenv
to set os's environment variables:
global.go
file
package global
import (
"log"
"os"
"strconv"
"github.com/joho/godotenv"
)
var (
SERVER_HOST string
SERVER_PORT int
)
func InitConfig() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
SERVER_HOST = os.Getenv("SERVER_HOST")
SERVER_PORT, _ = strconv.Atoi(os.Getenv("SERVER_PORT"))
}
main.go
file
package main
import(
G "path/to/config"
)
func init() {
G.InitConfig()
}
func main() {
G.Init()
}
You will still have to import "G" package in other packages to use the variables, but I think the best way to tackle global variables in Go (or any other languages) is to make use of environment variables.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论