Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created March 21, 2019 05:33
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/2074984df8ab06f522e11ae032b86401 to your computer and use it in GitHub Desktop.
Save parzibyte/2074984df8ab06f522e11ae032b86401 to your computer and use it in GitHub Desktop.
package main
/*
Codificar a JSON con Go
@author parzibyte
*/
import (
"encoding/json"
"fmt"
)
func main() {
// Cadenas
nombre := "Luis Cabrera Benito"
nombreComoJson, err := json.Marshal(nombre)
if err != nil {
fmt.Printf("Error codificando nombre: %v", err)
} else {
fmt.Println(string(nombreComoJson))
}
// Números
edad := 21
edadComoJson, err := json.Marshal(edad)
if err != nil {
fmt.Printf("Error codificando edad: %v", err)
} else {
fmt.Println(string(edadComoJson))
}
// Arreglos de todo tipo. En este caso de cadena...
videojuegos := []string{"Resident Evil", "Super Mario Bros", "Cuphead", "Halo"}
videojuegosComoJson, err := json.Marshal(videojuegos)
if err != nil{
fmt.Printf("Error codificando videojuegos: %v", err)
}else{
fmt.Println(string(videojuegosComoJson))
}
// Structs. Definimos unos para ver que soporta profundidad
type Raza struct {
Nombre, Pais string
}
type Mascota struct {
Nombre string
Edad int
Raza Raza
Amigos []string // Arreglo de strings
}
// Creamos algunos y los codificamos
raza := Raza{"Caniche", "Francia"}
amigos := []string{"Bichi", "Snowball", "Coqueta", "Cuco", "Golondrino"}
mascota := Mascota{"Maggie", 3, raza, amigos}
mascotaComoJson, err := json.Marshal(mascota)
if err != nil {
fmt.Printf("Error codificando mascota: %v", err)
} else {
fmt.Println(string(mascotaComoJson))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment