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)
}
}
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 1 year ago.
Improve this question
I am unable to parse json that has string keys and array as value ending up with json: Unmarshal(non-pointer map[string]interface {}) error.
package main
import (
"encoding/json"
"fmt"
)
func main() {
var s map[string]interface{}
err := json.Unmarshal([]byte("{\"a\":[1,2,3]}"), s)
if err != nil {
panic(err)
}
fmt.Println("Nice parse!")
}
https://go.dev/play/p/AXlF8I-f9-p
Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v. If v is nil or not a pointer, Unmarshal returns an InvalidUnmarshalError. Add &s as a parameter
err := json.Unmarshal([]byte("{\"a\":[1,2,3]}"), &s)
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 1 year ago.
Improve this question
Is it a normal behavior when parsing uint64 max value with strconv.ParseInt?
i, err := strconv.ParseInt("18446744073709551615", 10, 64)
fmt.Println(i, err)
I got an error: "strconv.ParseInt: parsing "18446744073709551615": value out of range", when maximum allowed value for uint64 is: 18446744073709551615
Can you explain such behavior?
https://golang.org/src/builtin/builtin.go?s=1026:1044#L26
Call ParseUint to parse an unsigned integer.
The ParseInt function parses signed integers. The maximum signed integer is 9223372036854775807.
Based the comments ,I reproduced your code as follows:
package main
import (
"fmt"
"strconv"
)
func main() {
i, err := strconv.ParseUint("18446744073709551615", 10, 64)
fmt.Println(i, err)
}
Output:
18446744073709551615 <nil>
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 am trying to write a program using Go which perform tuple or variable unpacking as below in python
url = ('https://www.amazon.com/War-Art-Through-Creative-Battles' '/dp/1936891026/?keywords=war+of+art')
domain , *rest , isbn = url.split("/")[2:-1]
So I have written code as below using Go
package main
import (
"fmt"
"strings"
)
func main() {
var a string
a = ("https://www.amazon.com/War-Art-Through-Creative-Battles" +
"/dp/1936891026/?keywords=war+of+art") // dont use ' quotes
fmt.Println(a)
split_a := strings.Split(a, "/")
fmt.Println(split_a)
var rest []string
var domain string
var isbn string
domain, rest, isbn = split_a[2:-1]
}
and getting cannot assign 1 values to 3 variables compiler WrongAssignCount.
I understand the error as it reflects what I am trying to achieve, I am trying to find out the methods to achieve this and finally thought of checking with others. Any suggestions much appreciated.
Thank you.
This is just string manipulation, so no one right answer. Since your source is a
URL, I would say net/url is a good starting point:
package main
import (
"net/url"
"path"
)
func main() {
p, e := url.Parse(
"https://www.amazon.com/" +
"War-Art-Through-Creative-Battles/dp/1936891026/?keywords=war+of+art",
)
if e != nil {
panic(e)
}
rest, isbn := path.Split(path.Clean(p.Path))
println(
p.Host == "www.amazon.com",
rest == "/War-Art-Through-Creative-Battles/dp/",
isbn == "1936891026",
)
}
https://golang.org/pkg/net/url
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.