Created
April 28, 2021 10:42
-
-
Save febriliankr/0a89f6ed83037d3da4631c458b0e51dd 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
func GETHandler(w http.ResponseWriter, r *http.Request) { | |
db := OpenConnection() | |
defer db.Close() | |
rows, err := db.Query("SELECT nama_lengkap FROM users") | |
if err != nil { | |
panic(err) | |
} | |
defer rows.Close() | |
var users []User | |
for rows.Next() { | |
var user User | |
rows.Scan(&user.Nama_lengkap) | |
users = append(users, user) | |
} | |
peopleBytes, _ := json.MarshalIndent(users, "", "\t") | |
w.Header().Set("Content-Type", "application/json") | |
w.Write(peopleBytes) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment