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

Time:10-23

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.

CodePudding user response:

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 
  • Related