Not able to convert err into go-sqlite3.Error [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 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
}
}

Related

golang -- cannot convert raw (variable of type []byte) to driver.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 5 months ago.
Improve this question
I'm trying to create a Value function for a jsonb database field, for use by go-pg.
func (m JSONMap) Value() (driver.Value, error) {
raw, err := json.Marshal(m)
if err != nil {
return driver.Value(nil), err
}
return driver.Value(raw), nil
}
This seems to be a pretty standard piece of code. Many more or less identical pieces of code can be found here: https://golang.hotexamples.com/examples/database.sql.driver/-/Value/golang-value-function-examples.html and very similar code can be found in the go-pg tests: https://github.com/go-pg/pg/blob/782c9d35ba243106ba6445fc753c3ac6a14c3324/conv_test.go
But no matter what I do, I get a compiler error: cannot convert raw (variable of type []byte) to driver.Value
If I replace return driver.Value(raw), nil with return driver.Value("ABC"), nil it seems to work just fine. However,
str := "ABC"
return driver.Value(str), nil
generates a compiler error.
I'm a little lost, any help is appreciated.

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

Ranging over golang map, inserting the new key [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 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]
}

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)

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

Resources