Created
December 11, 2018 00:22
-
-
Save parzibyte/35983501ecb91cfdbb0a057b8879490a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func obtenerContactos() ([]Contacto, error) { | |
contactos := []Contacto{} | |
db, err := obtenerBaseDeDatos() | |
if err != nil { | |
return nil, err | |
} | |
defer db.Close() | |
filas, err := db.Query("SELECT id, nombre, direccion, correo_electronico FROM agenda") | |
if err != nil { | |
return nil, err | |
} | |
// Si llegamos aquí, significa que no ocurrió ningún error | |
defer filas.Close() | |
// Aquí vamos a "mapear" lo que traiga la consulta en el while de más abajo | |
var c Contacto | |
// Recorrer todas las filas, en un "while" | |
for filas.Next() { | |
err = filas.Scan(&c.Id, &c.Nombre, &c.Direccion, &c.CorreoElectronico) | |
// Al escanear puede haber un error | |
if err != nil { | |
return nil, err | |
} | |
// Y si no, entonces agregamos lo leído al arreglo | |
contactos = append(contactos, c) | |
} | |
// Vacío o no, regresamos el arreglo de contactos | |
return contactos, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment