How to change when packages are excecuted [closed] - go

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have a package A in Go that looks like
package A
var Something int
func Do_something() {
Something = 100
}
where do_something in package A is called in main.go
package main
import "example.com/this_project/A"
func main() {
A.Do_something()
}
That works well, but now I add package B.
package B
import (
"example.com/this_project/A"
"fmt"
"strconv"
)
_ = do_something_else()
func do_something_else() {
fmt.Println("%s is the value of something", strconv.Itoa(A.Something))
}
func Some_other_function() {
do_whatever()
}
Some_other_function will also get called in main.go
package main
import "example.com/this_project/A"
import "example.com/this_project/B"
func main() {
A.Do_something()
B.Some_other_function()
}
When I run this program, I expect it to output
100
But instead, it outputs
0
I think this means that package B is running before main.go, so I tried using time.sleep, but it just caused the entire project to stop. Any suggestions will be appreciated.

All global variables are initialized before main starts running. Because of that, this line:
_ = do_something_else()
will run before main starts, which will print the current value of the variable, which is 0 at that point.

Related

missing go type for returned values [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
How come no warnings are issued for the following code?
$ cat ret.go
package main
import "fmt"
func foobar(x int, y int) (z, w int) {
if x+y > 100 {
_,w = 3,5
} else {
_,w = "MMM",9
}
return z,w
}
func main() {
var x int
_,x = foobar(42,13)
fmt.Println(x)
}
$ go build -gcflags=-l ret.go
For the least, the go compiler should know the size of z right?
In golang, you can define multiple variable in one line like next:
var identifier1, identifier2 type
So, z, w here both declared as int.
Additional, see this:
The return values of a function can be named in Golang
Then, if you not assign a value to z, it will has a default value of int, that is 0. So, no warning, the code is ok.

Cannot use _ as value [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I saw a piece of code used to print values passed in arguments:
package main
import "fmt"
import "os"
func main() {
for _, val := range os.Args[1:] {
fmt.Printf("%d %s \n", _ , val)
}
}
Original program had a note that _ holds index but was not printing it. When I tried to print index, I am getting below error:
./main.go:8:16: cannot use _ as value
What is the issue here?
_(underscore) in Golang is known as the Blank Identifier and it's value can't be used(it kind of doesn't hold any value).
Go doesn't allow you to have a unused variable therefore, original program used _ to drop the value and compile the program successfully. Use i instead of _ and run the program.
package main
import "fmt"
import "os"
func main() {
for i, val := range os.Args[1:] {
fmt.Printf("%d %s \n", i , val)
}
}

Why is the output of function f not seen? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Why The output is only : "Hello World". Why the func f() does not print ? The variables are available for this function. Thanks for your time.
package main
import "fmt"
var x string = "hello, World"
var y string = "hello, Joe"
func main() {
fmt.Println(x)
}
func f() {
fmt.Println(x, y)
}
The problem is quite simply that you never execute the f() function.
The variables are available for this function.
Yes, this is true, but irrelevant. It doesn't matter what is available to a function that is never executed.
If you want to execute f(), just call it:
func main() {
fmt.Println(x)
f()
}
See a working example in the playground.
Perhaps you are confused by the fact that main() (and init() when it exists) are automatically executed when a Go program starts. These two function names are special. init() functions (if they exist) are executed first, during program startup. Then main() is executed, and when main() exits, the program terminates.
All other functions must be called from within main() (or rarely init(), but avoid that unless you really know what you're doing.

Cannot change directory using os.Chdir() [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
os.Chdir() in golang is not working properly.
package main
import (
"fmt"
"os"
)
func main() {
command := "cd C:\\"
if err := os.Chdir(command[3:]); err != nil {
fmt.Println("Error:\tCould not move into the directory (%s)\n")
}
}
Outputs:
Error: Could not move into the directory
Am I doing something wrong or missing something?
You don't have minimal, reproducible example. See: How to create a Minimal, Reproducible Example.
Here is a minimum, reproducible example for your code, discarding all but essential code and printing input, output, and errors.
package main
import (
"fmt"
"os"
"runtime"
)
func main() {
fmt.Println(os.Getwd())
dir := `C:\`
if runtime.GOOS != "windows" {
dir = `/`
}
err := os.Chdir(dir)
fmt.Println(dir, err)
fmt.Println(os.Getwd())
}
Output:
Windows:
C:\Users\peter>go run chdir.go
C:\Users\peter <nil>
C:\ <nil>
C:\ <nil>
C:\Users\peter>
Linux:
$ go run chdir.go
/home/peter <nil>
/ <nil>
/ <nil>
$
It works.
Run it and compare it to your code.

Getter in GoLang [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have a custom type named ProtectedCustomType and I don't want the variables within that to be set or get directly by the caller, rather want a Getter / Setter methods to do that.
Below is my ProtectedCustomType
package custom
import "fmt"
type ProtectedCustomType struct {
name string
age string
phoneNumber int
}
func (pct *ProtectedCustomType) SetAge (age string) {
pct.age=age
fmt.Println(pct.age)
}
func (pct *ProtectedCustomType) GetAge () string {
return pct.age
}
And here is my main function
package main
import (
"fmt"
"./custom"
)
var print =fmt.Println
func structCheck2() {
pct := custom.ProtectedCustomType{}
pct.SetAge("23")
age:=pct.GetAge
print (age)
}
func main() {
structCheck2()
}
I am expecting it to print 23, but it is printing as 0x48b950
This (your code) takes the pct instance's GetAge method and stores it in a variable:
age:=pct.GetAge
This calls the GetAge method and stores its return value in a variable:
age:=pct.GetAge()
Consider taking the Tour of Go and reading the Go Specification to get a basic understanding of Go syntax.

Resources