type Employee struct { Name string Age int Salary float64 } func getEmployeeById(id int, dbmap *gorp.DbMap) (Employee, error) { var emp Employee err := dbmap.SelectOne(&emp, "SELECT * FROM employees WHERE id = ?", id) if err != nil { return Employee{}, err } return emp, nil } // Usage: emp, err:= getEmployeeById(1, dbmap) if err != nil { log.Fatalf("Error fetching employee from db: %v", err) } log.Printf("Employee with ID=%v: %+v", 1, emp)
type User struct { Id int64 Username string Email string } func getUserByUsername(username string, dbmap *gorp.DbMap) (User, error) { var user User err := dbmap.SelectOne(&user, "SELECT * FROM users WHERE username=?", username) if err != nil { return User{}, err } return user, nil } // Usage: user, err:= getUserByUsername("alice", dbmap) if err != nil { log.Fatalf("Error fetching user from db: %v", err) } log.Printf("User with username=%s: %+v", "alice", user)In this example, we define a struct User and a function getUserByUsername that takes a username and a DbMap object, and fetches the user from the DB by executing the SQL statement `SELECT * FROM users WHERE username=?`. The result of the query is mapped to a single user struct using the "SelectOne" method.