Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created December 11, 2018 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/3dc00a3cfd52ba7d9b59379d083aa116 to your computer and use it in GitHub Desktop.
Save parzibyte/3dc00a3cfd52ba7d9b59379d083aa116 to your computer and use it in GitHub Desktop.
func main() {
creditos := `==========================================================
CRUD de MySQL y GO
__ __ __
.-----.---.-.----.-----|__| |--.--.--| |_.-----.
| _ | _ | _|-- __| | _ | | | _| -__|
| __|___._|__| |_____|__|_____|___ |____|_____|
|__| |_____|
==========================================================`
fmt.Println(creditos)
menu := `¿Qué deseas hacer?
[1] -- Insertar
[2] -- Mostrar
[3] -- Actualizar
[4] -- Eliminar
[5] -- Salir
-----> `
var eleccion int
var c Contacto
for eleccion != 5 {
fmt.Print(menu)
fmt.Scanln(&eleccion)
scanner := bufio.NewScanner(os.Stdin)
switch eleccion {
case 1:
fmt.Println("Ingresa el nombre:")
if scanner.Scan() {
c.Nombre = scanner.Text()
}
fmt.Println("Ingresa la dirección:")
if scanner.Scan() {
c.Direccion = scanner.Text()
}
fmt.Println("Ingresa el correo electrónico:")
if scanner.Scan() {
c.CorreoElectronico = scanner.Text()
}
err := insertar(c)
if err != nil {
fmt.Printf("Error insertando: %v", err)
} else {
fmt.Println("Insertado correctamente")
}
case 2:
contactos, err := obtenerContactos()
if err != nil {
fmt.Printf("Error obteniendo contactos: %v", err)
} else {
for _, contacto := range contactos {
fmt.Println("====================")
fmt.Printf("Id: %d\n", contacto.Id)
fmt.Printf("Nombre: %s\n", contacto.Nombre)
fmt.Printf("Dirección: %s\n", contacto.Direccion)
fmt.Printf("E-mail: %s\n", contacto.CorreoElectronico)
}
}
case 3:
fmt.Println("Ingresa el id:")
fmt.Scanln(&c.Id)
fmt.Println("Ingresa el nuevo nombre:")
if scanner.Scan() {
c.Nombre = scanner.Text()
}
fmt.Println("Ingresa la nueva dirección:")
if scanner.Scan() {
c.Direccion = scanner.Text()
}
fmt.Println("Ingresa el nuevo correo electrónico:")
if scanner.Scan() {
c.CorreoElectronico = scanner.Text()
}
err := actualizar(c)
if err != nil {
fmt.Printf("Error actualizando: %v", err)
} else {
fmt.Println("Actualizado correctamente")
}
case 4:
fmt.Println("Ingresa el ID del contacto que deseas eliminar:")
fmt.Scanln(&c.Id)
err := eliminar(c)
if err != nil {
fmt.Printf("Error eliminando: %v", err)
} else {
fmt.Println("Eliminado correctamente")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment