Ranging over golang map, inserting the new key [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 12 months ago.
Improve this question
func hello() {
myMap := make(map[int]bool)
i := 0
myMap[0] = false
for val, ok := myMap[i]; ok && !val; {
fmt.Println("val", val)
i--
}
}
Why does this code run infinitely ? ideally it should once only once. can someone explain this ?

ok and val's are not update, this for update that values
for val, ok := myMap[i]; ok && !val; {
fmt.Println("val", val)
i--
val, ok = myMap[i]
}

Related

How can I change the variable inside the if statement of switch statement 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 6 months ago.
Improve this question
I am a newbie in Golang and now I have a requirement to change a value inside the if statement.
Here is my dummy code.
package main
func main() {
a := "hi"
pull_enable := true
switch a {
case "hi":
image_list := []float32{
0,
2,
}
for image:=0; image<len(image_list); image++{
if image == 0 {
pull_enable = true
break
}
}
}
}
I define a variable pull_enable outside of switch statement, and I want to change this variable value in the if statement, but when I built it, it encountered an issue below.
# command-line-arguments
pull_enable declared but not used
I am wondering how I can fix this issue. Is there any idea?
package main
import "fmt"
func main() {
a := "hi"
pullEnable := true
switch a {
case "hi":
image_list := []float32{
0,
2,
}
for image := 0; image < len(image_list); image++ {
if image == 0 {
pullEnable = true
break
}
}
}
fmt.Println(pullEnable)
}

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

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

Not able to convert err into go-sqlite3.Error [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
I am trying to convert an err in Go to go-sqlite3.Error, but it fails always.
Above image represents the snapshot of my debug windows, which shows that the err is of type go-sqlite3.Error
I am using below code to type cast.
import (
"github.com/mattn/go-sqlite3"
)
if err != nil {
if sqlite3Err, ok := err.(*sqlite3.Error); ok {
if sqlite3Err.Code == sqlite3.ErrConstraint && sqlite3Err.ExtendedCode == 1555 {
// SQLITE3 ERROR 1555 : PRIMARY KEY CONSTRAINT ERROR
return errors.New("Log Error")
}
}
try the following example. err.(*sqlite3.Error) is changed to err.(sqlite3.Error)
if sqlite3Err, ok := err.(sqlite3.Error); ok {
if sqlite3Err.Code == sqlite3.ErrConstraint &&
sqlite3Err.ExtendedCode == 1555 {
// logic
}
}

How to check words with the same characters where the words in one variable [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm thinking up about how I find the same characters in one variable looks like this:
var words string = "abab"
and then I want's to eliminate the same characters in that one variable and here's the output to be
Output:
ab
have any solution about this?
One solution can be the use of go map[] to track the taken characters.
sample code:
func main() {
s := "abcdaabcefgahccij"
newS := ""
taken := make(map[rune]int)
for _, value := range s {
if _, ok := taken[value]; !ok {
taken[value] = 1
newS += string(value)
}
}
fmt.Println(newS)
}
Output:
abcdefghij

Resources