Advertisement
rasyid03

main.go-beg-per7

Dec 9th, 2023
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 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("Server berjalan di port 8083") //ganti dengan 2 digit terakhir npm
  13. http.ListenAndServe(":8083", nil) //ganti dengan 2 digit terakhir npm
  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: "Faris",
  25. Kursus: "Fundamental Web",
  26. Foto: "img/gambar1.jpg",
  27. },
  28. {
  29. Nama: "Rasyid",
  30. Kursus: "Golang for Beginner",
  31. Foto: "img/gambar1.jpg",
  32. },
  33. }
  34.  
  35. func user(w http.ResponseWriter, r *http.Request) {
  36. w.Header().Set("Content-type", "application/json")
  37.  
  38. if r.Method == http.MethodGet {
  39. result, err := json.Marshal(data_mahasiswa)
  40.  
  41. if err != nil {
  42. http.Error(w, err.Error(), http.StatusInternalServerError)
  43. return
  44. }
  45.  
  46. w.Write(result)
  47. return
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement