Value assigned inside init doesnt maintain the value - go

I'm working on Golang and am a little confused about how the func init() works. Lets's say that I have 2 packages called main and pkg2
inside main I am trying to call a variable that is inside pkg2 but its giving me nil. Basically this is the structure:
Main Package:
import (
...
"github.com/myproject/config/pkg2"
)
func main () {
if pkg2.Myvariable == nil {
//it's nil. And it's entering in this conditional don't know why
}
}
PKG2 Package:
package pkg2
import (
...some imports...
)
var MyVariable
func init () {
MyVariable := "something"
//Here I assign a value to MyVariable
//I set an if here to check if it's executed
//and MyVariable get a value correctly
}
I also noticed that the init function is executed before I even call pkg2.Myvariable. So, briefly: inside main package it's given nil, but inside init the value is assigned correctly, why then it return to nil?
What Am I missing? Thank you!

I believe you should change := to =, because that way you are introducing a new var.

Related

Go package Global Variable usage within imports

I am creating a package that will be called from other services that I have in Go. If a service imports this package, and this package has global variables, will the component store the global variables in its' own memory?
So if my package looks something like this,
var global1 = ""
var global2 = ""
var global3 = ""
var global4 = ""
func setAllGlobalValues() error {
// some logic that checks if globals are nil
// if not setting it to a value after some computation.
// returns nil or an actual error.
}
func DoesSomethingUsingGlobalVars() (bool, error) {
// sets and uses the global vars.
// Does some sort of computation and returns a bool, nil or nil,error
}
Then in the service I would import this package and use the doesSomethingUsingGlobalVars function.
Will the component using this package store the global variables in its own memory? I can't really test it now with my services with the way things are set up so I'm just curious if anyone knows.
Essentially, will this work or will the global vars be nil each and every-time anything is called from a service that imports this package?
Thanks in advance all!
When your program imports this package, the globals will be set when you call SetAllGlobalValues(), and later, when you call DoesSomethingUsingGlobalVars(), those values will already be set. Note that the first letter of those function names must be capitalized so that they are exported and available for use by other packages. If the variables are not exported, as shown in your code snippet, you will not be able to access them directly from the caller.
It seems as if you are trying to reinvent objects. Instead of your code, do
something like this:
package some
import "fmt"
type Thing struct {
One string
Two string
Three string
Four string
}
func NewThing() Thing {
return Thing{"One", "Two", "Three", "Four"}
}
func (t Thing) Print() {
fmt.Println(t.One, t.Two, t.Three, t.Four)
}
Then, the "global variables" are only calculated once, when you call NewThing.

Why function in my package is not working

I have put a small code file in $GOPATH which is ~/go/src. Here I have made a folder mysrc and there I have kept a file mytest.go, which is as follows:
package mytest
import ("fmt")
func myfn(){
fmt.Println("My own fn")
}
I try to import above code with following file:
package main
import ("mysrc")
main(){
myfn()
}
When I try to run above file, I get error:
# command-line-arguments
./useMyfn.go:3:1: syntax error: non-declaration statement outside function body
Where is the problem and how can it be solved? Thanks for your help.
Edit: I corrected the main fn to func main() but now the errors are:
# command-line-arguments
./useMyfn.go:2:9: imported and not used: "mysrc" as mytest
./useMyfn.go:4:2: undefined: myfn
You need to do few things
I suggest to use a package name that the name is same with the folder name.
The myfn() function need to be exported. How to do it: simply set the first character of the function name to uppercase.
package mysrc // <-- 1
import (
"fmt"
)
func Myfn() { // <-- 2
fmt.Println("My own fn")
}
The func keyword is required before main() statement.
To access a function from other package, you need to write down the package name before the function name. In this context it'll be mysrc.Myfn().
package main
import (
"mysrc"
)
func main() { // <-- 3
mysrc.Myfn() // <-- 4
}

Viper, cannot get the value initialized outside the functions

I have a problem using a viper, I have assigned variable by viper method, so when I try to get value inside any function I have a null value. Does anybody have any idea why does it happen so? Any other variables initialization works fine, but not viper GetString method.
Structure:
main.go
package main
import (
"project/Model"
"github.com/spf13/viper"
...
)
func main() {
//Config handling
viper.SetConfigName("main")
viper.AddConfigPath("/config/")
err = viper.ReadInConfig()
...
}
Package Model
package Model
import ("github.com/spf13/viper"
...
)
var sqlhost = viper.GetString("db.host")
func foo() {
log.Println(sqlhost)
}
I suspect that doing package variable initialization as you show will lead to the viper configuration not being complete before performing the GetString(), and therefore the zero (empty) string value is what is returned. Without seeing how you initialize your viper config, I'm guessing that it's not in an init() function, but is in a main() function that hasn't run yet when the package variable is assigned.
You should probably retrieve the viper.GetString("db.host") inside the highest level function that needs it rather than during package initialization.

Golang derefencing function return value

I'm messing with the flag library, and found that this code does not work:
package main
import (
"fmt"
"flag"
)
var recursive bool
func init() {
recursive = *flag.Bool("r", false, "Search recursively")
}
func main() {
flag.Parse()
fmt.Printf("Recursive: %t \n\n", recursive)
flag.PrintDefaults()
}
But this does (I commented the three lines I changed):
package main
import (
"fmt"
"flag"
)
var recursive *bool //Changed to pointer type
func init() {
recursive = flag.Bool("r", false, "Search recursively") //Changed to not dereference function
}
func main() {
flag.Parse()
fmt.Printf("Recursive: %t \n\n", *recursive) //Changed to dereference variable
flag.PrintDefaults()
}
Why does this behave like this? Are functions not allowed to be dereferenced in Golang, or am I doing something else wrong?
The reason for this is because when you call flag.Bool(), it does not yet parse the command line arguments, it just defines a named flag and initializes it with the default value (which is false as you specified it).
Command line arguments are only parsed when you call flag.Parse(). If you use recursive bool, in the init() function the default false will be assigned to it. The flag package does not know about your recursive variable, so later when you call flag.Parse(), its value will not/ cannot be changed.
flag.Bool() returns a pointer to a bool variable which the flag package knows about, and later when you call flag.Parse() the pointed bool variable will be properly updated, so when you print the pointed valued after flag.Bool(), it will be the updated value which will be set based on your command line arguments.
Alternative: Register your variable
So you must store and use the pointer returned by flag.Bool() else you will only see the default value. Or you can let the flag package know about your recursive variable: you can tell the flag package that you what it to store the result into your recursive variable by telling this with the flag.BoolVar() function:
flag.BoolVar(&recursive, "r", false, "Search recursively")
Note that in this case there is no return value, becase you explicitly provided a pointer to a bool which you want the flag package to store the result to.

How to override variables from nested packages in go language?

How do I override variables in go from package ?
For example:
file1.go
package A
import "A/B"
var Arg1 = "Hello"
file2.go
package A/B
var Arg1 = "World"
How can I override arg1 in file1.go if arg1 exist in file2.go?
I'm not sure what you mean by "overriding" in this case. (Also let me assume file1.go is 'package a' and file2.go is 'package b')
If you mean to access Arg1, defined in package "b" inside/from within package "a", then eg.:
// In package "a"
x = Arg1 // references a.Arg1
y = b.Arg1 // references b.Arg1
However, nothing resembling overriding happens here. In package "a" both a's Arg1 and b's Arg1 are accessible as different entities; the later via a mandatory qualifier 'b'.
Are you trying to do something like this, where, for example, a specific location (USA), if present, overrides a general location (World)?
// file A/B.go
package B
var location = "USA"
func Location() string {
return location
}
// file A.go
package A
import "A/B"
var location = "World"
func Location() string {
loc := B.Location()
if len(loc) == 0 {
loc = location
}
return loc
}
// file main.go
package main
import (
"A"
"fmt"
)
func main() {
fmt.Println("Hello,", A.Location())
}
Output:
Hello, USA
If not, please provide a better and specific example of what you are trying to do.
You can't. Packages are self-contained.
If package A doesn't export arg1 (lowercase)
it is not visible -- and thus not set-able --
for an other package B.
BTW: "package A/B" looks pretty strange to me.
You could probably do it using a build flag :
go build -ldflags="-X 'package_path.variable_name=new_value'"
Couple of things to be aware of though :
In order to use ldflags, the value you want to change must exist and be a package level variable of type string. This variable can be either exported or unexported. The value cannot be a const or have its value set by the result of a function call
You'll find all the information needed in this excellent post from DO team

Resources