I just read the blog written by Rob Pike. I have a small question regarding this and may be I can be wrong too but would still like to get feedback and understand properly the Go.
In the blog there was a snippet code (which actually was written by #jxck_)
_, err = fd.Write(p0[a:b])
if err != nil {
return err
}
_, err = fd.Write(p1[c:d])
if err != nil {
return err
}
_, err = fd.Write(p2[e:f])
if err != nil {
return err
}
// and so on
a) As per my understanding the above code will return if error occurred at fd.Write(p0[a:b]), and will never execute fd.Write(p1[c:d]) , right?
And Rob suggested to write something like this
var err error
write := func(buf []byte) {
if err != nil {
return
}
_, err = w.Write(buf)
}
write(p0[a:b])
write(p1[c:d])
write(p2[e:f])
// and so on
if err != nil {
return err
}
b) Based on the above, looks like the error will return from the sub function. So this means if the error occurs at write(p0[a:b]) then still it will execute write(p1[c:d]), right? So this means logically both are not same, right?
Anybody explain.
No, they are the same. If an error occurs at fd.Write(p0[a:b]), the err variable will hold its value.
Now if you call write(p1[c:d]), then the write() func will first check if err != nil but since it already stores the error which occured in the previous call, it will return immediately and will not execute further code.
a) Yes, you are correct. If the error occures in the first write, it will return.
b) No. The write in this example is a closure. The err inside of it is the same as in the outer scope. So if the first write fails, the other will simply return, because the outer err is not nil anymore.
Related
i am trying to insert data after the connection, when i command the logic of INSERT... i was able to connect to the database, but when i uncommand them , i got error
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x40f8e2a]
here is my function :
func Connect() (*sql.DB, error) {
db, err := sql.Open("postgres", os.Getenv("PG_URL"))
if err != nil {
return nil, err
}
defer db.Close()
stmt, _ := db.Prepare("INSERT INTO users(name, email, password) VALUES(?,?,?)")
res, err := stmt.Exec("test", "test#mail.com", "12344")
if err != nil{
panic(err.Error())
}
fmt.Println(res)
fmt.Println("Successfully connected!")
return db, nil
}
I have tried to do the same thing also like this article go sql
and have the same issue
do I wrong implement this?
I bet a dollar/euro/frank that the NPE is on the line executing the prepared statement and that if you check the only error you ignored it won't be nil and it will tell you what's wrong.
I had the same problem with sqlite.
As Ivaylo Novakov described in his answer I had to log the err of the prepare statement (which i ignored like you before stmt, _)
For me it was running okay as long as i was developing but when I created my final binary i forgot to enable cgo).
The err got the hint:
Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub
I'm using firebird database driver from "github.com/nakagami/firebirdsql" with GO1.11 + FB2.5
But I can't get prepared SELECT to work, it throws "Error op_response:0" error when executing the 2nd QUERYROW(). Any ideas?
Is there any alternative driver? Or am I using incorrect driver?
func test1(tx *sql.Tx) {
sqlStr := "SELECT number FROM order WHERE id=?"
stmt, err := tx.Prepare(sqlStr)
if err != nil {
panic(err.Error())
}
var value string
err = stmt.QueryRow(123).Scan(&value)
if err != nil {
panic(err.Error())
}
fmt.Println(value)
err = stmt.QueryRow(200).Scan(&value)
if err != nil {
panic(err.Error())
}
fmt.Println(value)
}
Result:
INV20183121
panic: Error op_response:0
goroutine 1 [running]:
main.test1(0xc00009c000, 0xc0000a8200)
I can venture a guess. Looking at github.com/nakagami/firebirdsql sources, this seems to be the only code path which can produce this error. Looking here, it ignores any network errors returned by recvPackets, which means: any thing on the network socket breaks, and you get this error back (because that's what recvPackets returns in case of network error).
I'd suggest rebuilding your code with debugPrint code uncommented, and see what is actually going on on the network connection.
I have the code below:
if err == nil {
body, err := ioutil.ReadAll(response.Body)
if err == nil {
dataMap := &models.UserResponse{}
json.Unmarshal(body, &dataMap)
if dataMap.User == (models.UserId{}) {
err = fmt.Errorf("unauthorized")
fmt.Println(err) // when unathorized, prints unauthorized
}
}
}
fmt.Println(err) // always prints nil
The Println inside if dataMap.User ... prints "unauthorized", whereas the last Println always prints nil.
I have no idea why it happens, err is declared via var err error at the beginning of this function.
The cause is detailed in Spec: Short variable declaration:
Unlike regular variable declarations, a short variable declaration may redeclare variables provided they were originally declared earlier in the same block (or the parameter lists if the block is the function body) with the same type, and at least one of the non-blank variables is new. As a consequence, redeclaration can only appear in a multi-variable short declaration. Redeclaration does not introduce a new variable; it just assigns a new value to the original.
When using short variable declaration with multiple variables where one already exists, assignment will only happen to the existing variable if it was declared in the same block. Since in your case err variable existed before the if block, a new err variable will be created inside the if block, which has nothing to do with the "outsider" err variable (other than sharing its name). The outer err will be shadowed in the if block after the short variable declaration.
So what happens is that inside the if, you create a new err variable, and you assign a value to that, and you print that.
After the if statement, you will print the outer err variable whose value was not changed inside the if block, so it remains nil.
See this example:
var err error
fmt.Println("outside:", err) // nil
{
// A new err variable, shadows the outer:
i, err := strconv.Atoi("a")
fmt.Println("inside:", i, err) // 0 strconv.Aoti: parsing "a": invalid syntax
}
fmt.Println("outside:", err) // nil, because this was never changed
// Now this will change the "outer" err:
j, err := strconv.Atoi("a")
fmt.Println("outside:", j, err) // 0 strconv.Aoti: parsing "a": invalid syntax
Output (try it on the Go Playground):
outside: <nil>
inside: 0 strconv.Atoi: parsing "a": invalid syntax
outside: <nil>
outside: 0 strconv.Atoi: parsing "a": invalid syntax
If you want to use (assign to) the "outer" variable when also creating new variable(s), you can't use short variable declaration in a "nested" block but only simple assignment, in which case you also have to declare the other variables a priori, like in this example:
if err == nil {
var body []byte
body, err = ioutil.ReadAll(response.Body)
// ... rest...
}
See related questions:
Why it is possible to redefine err in multiple return statement in Go
Why there are two ways of declaring variables in Go, what's the difference and which to use?
You have created err inside block but you are printing same outside of block. Declare error outside of block and initialize it anywhere inside that will fetch the value when printing.
var err error
fmt.Println(err) // always prints nil
Background
I am using the github.com/jmoiron/sqlx golang package with a Postgres database.
I have the following wrapper function to run SQL code in a transaction:
func (s *postgresStore) runInTransaction(ctx context.Context, fn func(*sqlx.Tx) error) error {
tx, err := s.db.Beginx()
if err != nil {
return err
}
defer func() {
if err != nil {
tx.Rollback()
return
}
err = tx.Commit()
}()
err = fn(tx)
return err
}
Given this, consider the following code:
func (s *store) SampleFunc(ctx context.Context) error {
err := s.runInTransaction(ctx,func(tx *sqlx.Tx) error {
// Point A: Do some database work
if err := tx.Commit(); err != nil {
return err
}
// Point B: Do some more database work, which may return an error
})
}
Desired behavior
If there is an error at Point A, then the transaction should have done zero work
If there is an error at Point B, then the transaction should still have completed the work at Point A.
Problem with current code
The code does not work as intended at the moment, because I am committing the transaction twice (once in runInTransaction, once in SampleFunc).
A Possible Solution
Where I commit the transaction, I could instead run something like tx.Exec("SAVEPOINT my_savepoint"), then defer tx.Exec("ROLLBACK TO SAVEPOINT my_savepoint")
After the code at Point B, I could run: tx.Exec("RELEASE SAVEPOINT my_savepoint")
So, if the code at Point B runs without error, I will fail to ROLLBACK to my savepoint.
Problems with Possible Solution
I'm not sure if using savepoints will mess with the database/sql package's behavior. Also, my solution seems a bit messy -- surely there is a cleaner way to do this!
Multiple transactions
You can split your work in two transactions:
func (s *store) SampleFunc(ctx context.Context) error {
err := s.runInTransaction(ctx,func(tx *sqlx.Tx) error {
// Point A: Do some database work
})
if err != nil {
return err
}
return s.runInTransaction(ctx,func(tx *sqlx.Tx) error {
// Point B: Do some more database work, which may return an error
})
}
I had the problem alike: I had a lots of steps in one transaction.
After starting transaction:
BEGIN
In loop:
SAVEPOINT s1
Some actions ....
If I get an error: ROLLBACK TO SAVEPOINT s1
If OK go to next step
Finally COMMIT
This approach gives me ability to perform all steps one-by-one. If some steps got failed I can throw away only them, keeping others. And finally commit all "good" work.
I've put together a golang func that takes an uploaded file and saves it to folder.
Just before os.Create() I am getting the following error :
http: panic serving [::1]:64373: runtime error: index out of range
My golang function is:
func webUploadHandler(w http.ResponseWriter, r *http.Request) {
file, header, err := r.FormFile("file") // the FormFile function takes in the POST input id file
if err != nil {
fmt.Fprintln(w, err)
return
}
defer file.Close()
// My error comes here
messageId := r.URL.Query()["id"][0]
out, err := os.Create("./upload/" + messageId + ".mp3")
if err != nil {
fmt.Fprintf(w, "Unable to create the file for writing. Check your write access privilege")
return
}
defer out.Close()
// write the content from POST to the file
_, err = io.Copy(out, file)
if err != nil {
fmt.Fprintln(w, err)
}
fmt.Fprintf(w,"File uploaded successfully : ")
fmt.Fprintf(w, header.Filename)
}
any ideas? much appreciate
You should at least check if r.URL.Query()["id"] has actually one element.
If len(r.URL.Query()["id"]), you could consider not accessing the index 0.
Easier, Ainar-G suggests in the comments to use the Get() method
Get gets the first value associated with the given key.
If there are no values associated with the key, Get returns the empty string.
To access multiple values, use the map directly.