Skip to content

Instantly share code, notes, and snippets.

@DrumRobot
Last active June 13, 2022 06:16
Show Gist options
  • Select an option

  • Save DrumRobot/7dfb495c915c745060b51bfac7a1becb to your computer and use it in GitHub Desktop.

Select an option

Save DrumRobot/7dfb495c915c745060b51bfac7a1becb to your computer and use it in GitHub Desktop.
Fetch data via go-gecko

Fetch data via go-gecko

Go Generic features require version 1.18

# go 구버전이 설치된 상태에서 1.18버전 설치
go install golang.org/dl/go1.18.3@latest
~/go/bin/go1.18.3 download
ln -s ~/go/bin/go1.18.3 ~/bin/go # 또는 ~/bin/go.exe(윈10), PATH 우선순위가 기존 go 보다 높아야함

go-gecko 설치

go get gecko
module gecko
go 1.18
require (
github.com/superoo7/go-gecko v1.0.0
)
package main
import (
"log"
"net/http"
"time"
coingecko "github.com/superoo7/go-gecko/v3"
types "github.com/superoo7/go-gecko/v3/types"
)
type Any = interface{}
type Result = types.CoinList
type Token = types.CoinsListItem
/** Generic 매핑 함수 */
func Map[T comparable](vs []T, f func(T) T) []T {
vsm := make([]T, len(vs))
for i, v := range vs {
vsm[i] = f(v)
}
return vsm
}
/** Item 저장 */
func storeCoin(coin Token) Token {
log.Printf("%+v\n", coin)
return coin
}
func main() {
httpClient := &http.Client{
Timeout: time.Second * 10,
}
client := coingecko.NewClient(httpClient)
done := make(chan *Result)
exit := make(chan error)
go func() {
for { // 비동기 루프
coins, err := client.CoinsList()
if err != nil {
exit <- err
break
}
done <- coins
time.Sleep(time.Second * 2)
}
}()
for {
select {
case coins := <-done:
Map(*coins, storeCoin)
case err := <-exit:
log.Fatal(err)
break
}
}
}
@DrumRobot
Copy link
Author

실행 테스트를 위해 패키지를 main으로 수정했습니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment