Advertisement
rasyid03

Untitled

Jul 13th, 2023 (edited)
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. )
  8.  
  9. func main() {
  10. http.Handle("/", http.FileServer(http.Dir("polymer")))
  11. http.HandleFunc("/api/mahasiswa", user)
  12. fmt.Println("web Berjalan dengan port 5020")
  13. http.ListenAndServe(":5024", nil)
  14. }
  15.  
  16. type lepkom struct {
  17. Nama string `json:"nama_mahasiswa"`
  18. Kursus string `json:"kursus_mahasiswa"`
  19. Foto string `json:"foto_mahasiswa"`
  20. }
  21.  
  22. var data_mahasiswa = []lepkom{
  23. {
  24. Nama: "Nanang",
  25. Kursus: "Golang For Beginer",
  26. Foto: "img/gambar1.jpg",
  27. },
  28. }
  29.  
  30. func user(w http.ResponseWriter, r *http.Request) {
  31. w.Header().Set("Content-type", "application/json")
  32.  
  33. if r.Method == http.MethodGet {
  34. result, err := json.Marshal(data_mahasiswa)
  35.  
  36. if err != nil {
  37. http.Error(w, err.Error(), http.StatusInternalServerError)
  38. return
  39. }
  40. w.Write(result)
  41. return
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement