Skip to content

Instantly share code, notes, and snippets.

@khannedy
Created April 24, 2022 22:37
Show Gist options
  • Save khannedy/d788b386297caf04b39640bec43f3131 to your computer and use it in GitHub Desktop.
Save khannedy/d788b386297caf04b39640bec43f3131 to your computer and use it in GitHub Desktop.
Golang Web with Write File
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
)
func main() {
port := os.Getenv("APP_PORT")
fmt.Println("Run app in port : " + port)
http.HandleFunc("/", HelloServer)
http.ListenAndServe(":"+port, nil)
}
func HelloServer(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %s", r.URL.Path[1:])
dataString := "Hello " + r.URL.Path[1:]
dataBytes := []byte(dataString)
destination := os.Getenv("APP_DATA")
file := destination + "/" + r.URL.Path[1:] + ".txt"
err := ioutil.WriteFile(file, dataBytes, 0666)
if err != nil {
panic(err)
}
fmt.Println("DONE Write File : " + file)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment