Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created March 17, 2021 18: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/de729184e9bddd3b44edff2305ba6c17 to your computer and use it in GitHub Desktop.
Save parzibyte/de729184e9bddd3b44edff2305ba6c17 to your computer and use it in GitHub Desktop.
/*
https://parzibyte.me/blog
*/
package main
import "fmt"
import "net/http"
import "os"
import "io"
func descargarArchivoDeInternet(url string) (string, error) {
/*
En este caso voy a descargar una imagen PNG
*/
nombreArchivoSalida := "imagen.png"
respuesta, err := http.Get(url)
if err != nil {
return "", err
}
defer respuesta.Body.Close()
archivoSalida, err := os.Create(nombreArchivoSalida)
if err != nil {
return "", err
}
defer archivoSalida.Close()
_, err = io.Copy(archivoSalida, respuesta.Body)
return nombreArchivoSalida, err
}
func main() {
fmt.Println("Descargando...")
nombre, err := descargarArchivoDeInternet("https://github.com/parzibyte.png")
if err == nil {
fmt.Printf("Se descargó como %s", nombre)
} else {
fmt.Printf("Error: %v", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment