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.
Related
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 11 months ago.
Improve this question
I have the following golang code which trying to access elements on array my expectation to print bxar ,but it throw error any idea?
package main
import (
"encoding/json"
"fmt"
)
type Data struct {
Args struct {
Foo string
}
}
func main() {
in := `[{"args": {"foo": "bar"}},{"args": {"foo": "bxar"}}]}`
var d []Data
json.Unmarshal([]byte(in), &d)
fmt.Println("Foo:", d[1].Args.Foo)
//fmt.Printf("Result: %+v", d)
}
The reason it does not work is a typo. There is one too many } in your JSON:
Before:
`[{"args": {"foo": "bar"}},{"args": {"foo": "bxar"}}]}`
After:
`[{"args": {"foo": "bar"}},{"args": {"foo": "bxar"}}]`
See this playground: https://go.dev/play/p/sL8Cx8lF6WR
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.
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 want to check if time.Now is after another time.Time in Go.
person.CreatedAt is time.Time
if time.Now > person.CreatedAt {
fmt.Println("time.Now is after person.CreatedAt")
}
Here simple example how you can check it:
package main
import (
"fmt"
"time"
)
func main() {
dateFormat := "2006-01-02"
personCreatedAt, err := time.Parse(dateFormat, "2020-01-01")
if err != nil {
// error handling...
}
ok := time.Now().After(personCreatedAt)
fmt.Println(ok)
}
Result will be: true
You can use time.After, time.Before and time.Equal to compare times:
if time.Now().After(person.CreatedAt) {
fmt.Println("time.Now is after person.CreatedAt")
}
To check if a time.Time variable is empty use time.IsZero
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'm trying to test a URL via a Go function. In our environment, we have our hostnames set up per environment, like https://www/examplesite20193.domain.org Here is a simple example of what I've got:
package main
import (
"fmt"
"net/http"
"log"
)
func main() {
versionMaj := "2019"
versionMin := "3"
endpoint := versionMaj+versionMin
resp, err:= http.Get("https://www.examplesite%s.domain.org", endpoint)
if err != nil {
log.Fatal(err)
}
fmt.Println("HTTP Response Status:", resp.StatusCode, http.StatusText(resp.StatusCode))
}
When I test this, it's stating too many arguments in call to http.Get have (string, string) want (string).
Is there a way to pass in a parameter, like the endpoint one I have specified?
Probably you're looking to something like this:
resp, err:= http.Get(fmt.Sprintf("https://www.examplesite%s.domain.org", endpoint))
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)
}
}