strconv.ParseInt: parsing "18446744073709551615": value out of range [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
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>

Related

How to parse map[string]interface{} [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
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)

Go pointer subtraction [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
When I run this program it looks like the value of the two pointers is 16 bytes (x’10”) bytes apart but how can that be if the first string is over 16 bytes long? Or am I looking at this incorrectly?
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println("Hello, playground")
x := "string 4"
xx := "string2"
y := "string3"
xptr := &x
fmt.Println(xptr)
fmt.Println(*xptr)
xxptr := &xx
fmt.Println(xxptr)
fmt.Println(*xxptr)
fmt.Println("hey")
fmt.Println("hey")
fmt.Println("hey")
fmt.Println("hhey")
fmt.Println("hey")
yptr := &y
fmt.Println(yptr, *yptr)
xxx := math.Pow(2,3)
fmt.Printf("%.6f",xxx)
}
The program prints the addresses of the string variables, not the address of the array of bytes backing the strings. A string variable contains a pointer to the string data and the string length. The structure of a string variable is reflected in the reflect.StringHeader type:
type StringHeader struct {
Data uintptr
Len int
}
See Go Data Structures for a detailed description of the string memory layout in Go.
On a 64-bit architecture, the size of a string variable is 16 bytes.
Use the unsafe package to extract the pointer to the string data:
xh := (*reflect.StringHeader)(unsafe.Pointer(&x))
xxh := (*reflect.StringHeader)(unsafe.Pointer(&xx))
fmt.Printf("0x%x %d\n", xh.Data, xh.Len) // prints 0x4c0648 26
fmt.Printf("0x%x %d\n", xxh.Data, xxh.Len) // prints 0x4bcfd4 7

How to check if one time.Now is after another time.Time [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 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

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)
}
}

Go newbie: Getting "mismatched types *int and int" error when trying to compare reference to static integer [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'm learning Go and trying to use reference to integer value in if-clause.
package main
import (
"fmt"
)
func main() {
a := 19
b := &a
if b > 10 {
fmt.Println("Its bigger")
}
}
This gives error message for type mismath.
How could I successfully compare value which b is referencing. In my training code I'm reading command line arguments with flags, but I suppose this example is reprex.
How should I compare when havin only reference available?
Here b is a pointer of int means *int. You can't compare *int type with int type.
Use *b to dereference to get the value and then compare with constant value.
if *b > 10 {
fmt.Println("Its bigger")
}

Resources