'go'에 해당되는 글 7건

go 프로젝트 세팅

2024. 2. 26. 15:07

보호되어 있는 글입니다.
내용을 보시려면 비밀번호를 입력하세요.

출처 : https://api.raritysniper.com/public/collection/cryptopunks/id/8348

 

package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)

type User struct {
Nftid       int     `json:"nftId"`
Rank        int     `json:"rank"`
Rarityscore float64 `json:"rarityScore"`
}

func customGet() {
// Request 객체 생성
req, err := http.NewRequest("GET", "https://api.raritysniper.com/public/collection/cryptopunks/id/8348", nil)
if err != nil {
panic(err)
}

//헤더 추가
req.Header.Add("User-Agent", "Golang-test")

// Client 객체에서 Request 실행
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()

// 결과 출력
bytes, _ := ioutil.ReadAll(resp.Body)
str := string(bytes) //바이트를 문자열로

var u User
json.Unmarshal([]byte(str), &u)
fmt.Printf("%+v\n", u) // {Name:gopher Age:7}

//fmt.Println(str)

}

func main() {
customGet()

}

 

 

 

 

 

 

'go' 카테고리의 다른 글

go 프로젝트 세팅  (0) 2024.02.26
JSON Transfer  (0) 2022.04.14
Web Handler  (0) 2022.04.14
go언어 설치  (0) 2022.04.14
routing module  (0) 2022.04.14
블로그 이미지

iesay

,

JSON Transfer

go 2022. 4. 14. 13:56

https://github.com/tuckersGo/goWeb/blob/master/web2/main.go

 

https://www.youtube.com/watch?v=vOW0j6hd-Rg&ab_channel=TuckerProgramming

 

package main

import (
  "encoding/json"
  "fmt"
  "net/http"
  "time"
)

type User struct {
  FirstName string
  LastName  string
  Email     string
  CreatedAt time.Time
}

type fooHandler struct{}

func (f *fooHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  user := new(User)
  err := json.NewDecoder(r.Body).Decode(user)
  if err != nil {
    w.WriteHeader(http.StatusBadRequest)
    fmt.Fprint(w, "Bad Request: ", err)
    return
  }
  user.CreatedAt = time.Now()

  data, _ := json.Marshal(user)
  w.WriteHeader(http.StatusOK)
  fmt.Fprint(w, string(data))
}

func barHandler(w http.ResponseWriter, r *http.Request) {
  name := r.URL.Query().Get("name")
  if name == "" {
    name = "World"
  }
  fmt.Fprintf(w, "Hello %s!", name)
}

func main() {
  mux := http.NewServeMux()
  mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hello World")
  })

  mux.HandleFunc("/bar", barHandler)

  mux.Handle("/foo", &fooHandler{})

  http.ListenAndServe(":3000", mux)
}

 

URL에서 name값을 출력 

 

http://192.168.0.6:3000/bar?name='aaaaaa'

'go' 카테고리의 다른 글

go 프로젝트 세팅  (0) 2024.02.26
cryptopunks raritysniper json 파싱  (0) 2022.04.15
Web Handler  (0) 2022.04.14
go언어 설치  (0) 2022.04.14
routing module  (0) 2022.04.14
블로그 이미지

iesay

,

Web Handler

go 2022. 4. 14. 13:41

출처 : https://www.youtube.com/watch?v=4Oml8mbBXgo&ab_channel=TuckerProgramming 

https://github.com/tuckersGo/goWeb/blob/master/web1/main.go

위 코드로 공부겸 샘플 예제 실습

 

package main

import (
"fmt"
"net/http"
)

type fooHandler struct{}

func (f *fooHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello Foo!")
}

func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello World")
})

http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello Bar!")
})

http.Handle("/foo", &fooHandler{})

http.ListenAndServe(":3000", nil)
}

 

인덱스 경로 

http://192.168.0.6:3000/

/bar 경로 

http://192.168.0.6:3000/bar

/foo 경로 fooHandler인스턴스 메서드 실행 

http://192.168.0.6:3000/foo

 

'go' 카테고리의 다른 글

cryptopunks raritysniper json 파싱  (0) 2022.04.15
JSON Transfer  (0) 2022.04.14
go언어 설치  (0) 2022.04.14
routing module  (0) 2022.04.14
golang 개발환경 구축  (0) 2021.06.21
블로그 이미지

iesay

,

go언어 설치

go 2022. 4. 14. 13:34

https://go.dev/doc/install

 

go언어 다운로드

#wget https://go.dev/dl/go1.18.1.linux-amd64.tar.gz

기존 설치된 go 삭제 압축 품

#rm -rf /usr/local/go && tar -C /usr/local -xzf go1.18.1.linux-amd64.tar.gz

환경변수 등록

#vi /etc/profile

export PATH=$PATH:/usr/local/go/bin

#reboot

#go version 

 

'go' 카테고리의 다른 글

cryptopunks raritysniper json 파싱  (0) 2022.04.15
JSON Transfer  (0) 2022.04.14
Web Handler  (0) 2022.04.14
routing module  (0) 2022.04.14
golang 개발환경 구축  (0) 2021.06.21
블로그 이미지

iesay

,

routing module

go 2022. 4. 14. 10:46

출처 : https://spectrumdig.blogspot.com/2012/02/routing-module-handler.html?m=0 

 

package main

import (
  "fmt"
)

type Handler func(method string)

type Route struct {
  Method,Pattern string
}

type Routes struct {
  Routes []Route
}
func (r Routes) routeCount() int {
  return len(r.Routes)
}
func (r *Routes) addRoute(method, pattern string, handler Handler) {
  r.Routes=append(r.Routes, Route{Method:method, Pattern:pattern})
  fmt.Printf("route Count : %d\n", len(r.Routes))
  handler(method)
}
func (r *Routes) GET(pattern string, handler Handler) {
  r.addRoute("GET", pattern, handler)
}
func (r *Routes) POST(pattern string, handler Handler) {
  r.addRoute("POST", pattern, handler)
}
func (r *Routes) PUT(pattern string, handler Handler) {
  r.addRoute("PUT", pattern, handler)
}
func (r *Routes) DELETE(pattern string, handler Handler) {
  r.addRoute("DELETE", pattern, handler)
}
func handler(method string) {
  fmt.Printf("%s handler executed \n", method)
}

func main() {
  r:=new(Routes)
  r.GET("/user/:user", handler)
  r.PUT("/add/:user", handler)
  r.POST("/update/:user", handler)
  r.DELETE("/delete/:user", handler)
  for _,v:=range r.Routes {
    fmt.Printf("%s:%s\n",v.Method, v.Pattern)
  }
}

 

 

'go' 카테고리의 다른 글

cryptopunks raritysniper json 파싱  (0) 2022.04.15
JSON Transfer  (0) 2022.04.14
Web Handler  (0) 2022.04.14
go언어 설치  (0) 2022.04.14
golang 개발환경 구축  (0) 2021.06.21
블로그 이미지

iesay

,

golang 개발환경 구축

go 2021. 6. 21. 17:33




wget https://go.dev/dl/go1.18.4.linux-amd64.tar.gz

tar -zxvf go1.18.4.linux-amd64.tar.gz

mv go /usr/local

echo 'GOPATH="/usr/local/go"' >> ~/.profile

echo 'PATH="$PATH:$GOPATH/bin"' >> ~/.profile

source .profile

 

'go' 카테고리의 다른 글

cryptopunks raritysniper json 파싱  (0) 2022.04.15
JSON Transfer  (0) 2022.04.14
Web Handler  (0) 2022.04.14
go언어 설치  (0) 2022.04.14
routing module  (0) 2022.04.14
블로그 이미지

iesay

,