Any Coder
Any Coder

Reputation: 81

How can I query all rows out of my table with GORM?

This is my users table:

id name pass
1 Test 0a2f60e41b1d3d302c0af17bc65d4f48
2 SecUsr 40597ff5ca18da3a91e0ee330496bc77

How can I get all rows with GORM? If I use db.Find() method, I'll get only the first row.

Upvotes: 7

Views: 15400

Answers (1)

Kedar U Godkhindi
Kedar U Godkhindi

Reputation: 126

Use the find function in the following way

   var users []User
   // Get all records
   result := db.Find(&users)
   // SELECT * FROM users;
   result.RowsAffected // returns found records count, equals `len(users)`
   result.Error        // returns error 

Upvotes: 11

Related Questions