Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In go, how do I make global variables

Tags:

go

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

like image 776
liam Avatar asked Nov 24 '15 23:11

liam


People also ask

Are there global variables in Go?

Every global variable in the go language defined at the top of the function. Defining the global variable in go language will be similar to that of the local or any other variable; the only difference is that we define it outside the functions.

Why not use global variables Golang?

Why we should not use global variable in Golang? Global variables have no access-control. We can access and change these variables in any part of the code. At the project grows, it becomes difficult to remember the actual reason for every possible use.

What is the default value of global variable in Go?

Local and global variables are initialized to their default value, which is 0; while pointers are initialized to nil.


2 Answers

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)
like image 66
evanmcdonnal Avatar answered Sep 30 '22 14:09

evanmcdonnal


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.

like image 38
Pandemonium Avatar answered Sep 30 '22 14:09

Pandemonium