-
-
Save parzibyte/de729184e9bddd3b44edff2305ba6c17 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
/* | |
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