1 post tagged with "go"

View All Tags

Go ์‹œ์ž‘ํ•˜๊ธฐ - 01

๋“ค์–ด๊ฐ€๋ฉฐ#

์ƒˆ๋กœ์šด ๊ฒƒ์— ๋Œ€ํ•œ ๋ฐฐ์›€์€ ์‚ถ์„ ํ’์š”๋กญ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ์ต์ˆ™ํ•จ์—์„œ ๋ฒ—์–ด๋‚˜๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋˜ ๋‹ค๋ฅธ ์‹œ๊ฐ์„ ํฌํ•จํ•˜์—ฌ ๋งŽ์€ ๊ฒƒ์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์ฃผ๊ด€์ ์ธ ๋ฐฐ์šฐ๊ณ  ์‹ถ์€ ์–ธ์–ด ๋žญํ‚น 1์œ„์ธ Golang ์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ์ฐจ๊ทผ์ฐจ๊ทผ ๋ฐฐ์›Œ๋‚˜๊ฐ€๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ธ€์€ ๋ˆ„๊ตฐ๊ฐ€์—๊ฒŒ Golang ์— ๋Œ€ํ•œ ์ •๋ณด ์ „๋‹ฌ์„ ๋ชฉํ‘œ๋กœ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ๋‚ด๊ฐ€ ์–ธ์–ด๋ฅผ ๋ฐฐ์›Œ๋‚˜๊ฐ€๋Š” ๊ณผ์ •์„ ๋‹ด์€ ๊ฐœ๋ฐœ ํ•™์Šต ๋กœ๊น…์ž…๋‹ˆ๋‹ค.

๋ชจ๋“  ๊ณผ์ •์— ๋Œ€ํ•œ ์ฝ”๋“œ๋Š” GitHub ์— ๊ณต๊ฐœ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.

๊ฐ„๋‹จํ•˜๊ฒŒ ์‹œ์ž‘ํ•˜๊ธฐ#

๊ฐœ๋ฐœ ํ™˜๊ฒฝ ๊ตฌ์„ฑ#

๋จผ์ € Go๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด Go ๋ฅผ ์„ค์น˜ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ์„ค์น˜๋Š” ๋„ˆ๋ฌด๋‚˜ ๊ฐ„๋‹จํ•ด์„œ ์—ฌ๊ธฐ์—์„œ๋Š” ๋”ฐ๋กœ ๋‹ค๋ฃจ์ง€ ์•Š๊ฒ ์Šต๋‹ˆ๋‹ค.

์ด์ œ ์–ด๋–ค ์—๋””ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ฝ”๋”ฉ์„ ํ• ์ง€ ๊ฒฐ์ •ํ•ด์•ผํ•˜๋Š”๋ฐ์š”. ์ €๋Š” ์ธํ…”๋ฆฌ์ œ์ด์˜ ๋…ธ์˜ˆ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ธํ…”๋ฆฌ์ œ์ด๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” Go ๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋ฐ, ํ”Œ๋Ÿฌ๊ทธ์ธ๋งŒ ์„ค์น˜ํ•˜๋ฉด GoLand (JetBrain ์˜ Go ์ „์šฉ IDE) ์˜ ๋ชจ๋“  ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•˜๋‹ˆ ํ™˜ํ˜ธ์„ฑ์„ ์ง€๋ฅด๋ฉฐ ์–ด์„œ ์„ค์น˜ํ•ด์ค๋‹ˆ๋‹ค.

Preferences 2021-08-15 17-13-09.png

์„ค์น˜๊ฐ€ ๋˜์—ˆ์œผ๋ฉด, ๋นจ๋ฆฌ ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ฃ . ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“œ๋Š” ์ˆœ๊ฐ„๋ถ€ํ„ฐ ๊ทธ ํ”„๋กœ์ ํŠธ์˜ ์—ญ์‚ฌ๋Š” ์‹œ์ž‘๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ์—ญ์‚ฌ๋Š” commit ์œผ๋กœ ๊ธฐ๋ก๋˜์ฃ . ์ขŒ์ธก์—์„œ Go ๋ฅผ ์„ ํƒํ•˜๊ณ  ๋‹ค์Œ์„ ๋ˆŒ๋Ÿฌ ํ”„๋กœ์ ํŠธ ์ด๋ฆ„์„ ์ง€์ •ํ•˜๊ณ  Finish ๋ฅผ ๋ˆ„๋ฅด๋ฉด ํ”„๋กœ์ ํŠธ๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. Go ๋ฒ„์ „์ด ์—…๋ฐ์ดํŠธ๋˜๋ฉด์„œ GOPATH ๋ฐฉ์‹์— ํ”„๋กœ์ ํŠธ๋Š” ์ž˜ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์œผ๋กœ ์•Œ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

New Project 2021-08-15 17-20-32.png

์ž ์ƒˆ๋กœ์šด ์„ธ์ƒ์„ ๋งŒ๋‚  ์ค€๋น„๊ฐ€ ๋˜์—ˆ๋‹ค๋ฉด ์ธ์‚ฌ๋ฅผ ํ•˜๋ฉฐ ์‹œ์ž‘ํ•ด๋ด…๋‹ˆ๋‹ค. main.go ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  main ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•œ ๋’ค ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

package main
import "fmt"
func main() {
fmt.Print("Hello")
}
Hello, Go World!
Process finished with the exit code 0

๊ฐœ์ธ์ ์œผ๋กœ๋Š” ์ธ์‚ฌ๋งŒ ํ•œ ๋‹ค์„ฏ๋ฒˆ์งธ ํ•˜๋Š” ๊ฒƒ ๊ฐ™๋„ค์š” ๐Ÿ˜

์™ธ๋ถ€ ๋ฆฌ์†Œ์Šค ๊ฐ€์ ธ์˜ค๊ธฐ#

์ธ์‚ฌ๋งŒ ํ•˜๊ณ  ๋๋‚ด๊ธฐ๋Š” ์•„์‰ฌ์šฐ๋‹ˆ ํ•œ ๋ฐœ์ž๊ตญ๋งŒ ๋” ๊ฐ€๋ณผ๊นŒ์š”? ์ถ”ํ›„์— ์˜๋ฏธ์žˆ๋Š” ์•ฑ์„ ๋งŒ๋“ค๋ ค๋ฉด ์™ธ๋ถ€์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ค๋Š”๊ฒŒ ํ•„์ˆ˜์ ์ž…๋‹ˆ๋‹ค. ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋‹ค๋ฉด, ๊ทธ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•ด์ฃผ๋Š” ์„œ๋น„์Šค๋ฅผ ์ฐพ์•„๋ณด๊ณ  ๊ทธ ์„œ๋น„์Šค๊ฐ€ API ๋ฅผ ์ง€์›ํ•˜๋Š”์ง€ ํ™•์ธํ•ด๋ณด์„ธ์š”. ๋Œ€๋ถ€๋ถ„์˜ ์„œ๋น„์Šค๋“ค์€ API ๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

API ๋ฌธ์„œ ํ™•์ธ#

์—ฌ๊ธฐ์„œ๋Š” ์š”์ฆ˜ ํ•ซํ•œ ์•”ํ˜ธํ™”ํ์˜ ํ˜„์žฌ ์‹œ์„ธ๋ฅผ ๊ฐ€์ง€๊ณ  ์™€๋ณด์ฃ . ์ €๋Š” ์—…๋น„ํŠธ ๊ฑฐ๋ž˜์†Œ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์—…๋น„ํŠธ API ๋ฌธ์„œ ์—์„œ ํ˜„์žฌ๊ฐ€ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ์—”๋“œํฌ์ธํŠธ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ์ฝ”๋”ฉํ•˜๊ธฐ ์ „์— ์ •๋ง๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ๋‚ด๋ ค์˜ค๋Š”์ง€ ํ™•์ธํ•ด๋ณผ๊นŒ์š”?

curl --request GET \
--url https://api.upbit.com/v1/ticker\?markets\=KRW-BTC \
--header 'Accept: application/json'
[{"market":"KRW-BTC","trade_date":"20210815","trade_time":"075919","trade_date_kst":"20210815","trade_time_kst":"165919","trade_timestamp":1629014359000,"opening_price":54817000.00000000,"high_price":55166000.0,"low_price":53420000.0,"trade_price":53614000.0,"prev_closing_price":54812000.00000000,"change":"FALL","change_price":1198000.00000000,"change_rate":0.0218565278,"signed_change_price":-1198000.00000000,"signed_change_rate":-0.0218565278,"trade_volume":0.022,"acc_trade_price":213592738229.775190000,"acc_trade_price_24h":559827073056.69978000,"acc_trade_volume":3918.44716516,"acc_trade_volume_24h":10269.53880782,"highest_52_week_price":81994000.00000000,"highest_52_week_date":"2021-04-14","lowest_52_week_price":11860000.00000000,"lowest_52_week_date":"2020-09-07","timestamp":1629014359797}]

์ž˜ ๊ฐ€์ง€๊ณ  ์˜ค๋„ค์š”. ๊ฐœ๋ฐœํ•˜๊ธฐ์— ์•ž์„œ ์ด๋Ÿฐ ๊ณผ์ •์€ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ๋‚ด๊ฐ€ ์ฝ”๋”ฉ์„ ์ž˜๋ชปํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ชป ๊ฐ€์ง€๊ณ  ์˜จ ๊ฑด์ง€, ์•„๋‹ˆ๋ฉด ์„œ๋น„์Šค ์ž์ฒด์ ์œผ๋กœ ๋ฌธ์ œ๊ฐ€ ์žˆ์–ด์„œ ๋ชป๊ฐ€์ง€๊ณ  ์˜จ ๊ฒƒ์ธ์ง€ ํ™•์ธ์ด ํ•„์š”ํ•˜์ฃ . ์„œ๋น„์Šค๋Š” ๋ฌธ์ œ๊ฐ€ ์—†์œผ๋‹ˆ ์ด์ œ ์ œ๊ฐ€ ์ฝ”๋”ฉ๋งŒ ์ž˜ํ•˜๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ค๊ฒ ๋„ค์š”. ๐Ÿคฉ

์‚ฌ์šฉํ•  ํŒจํ‚ค์ง€ ์ฐพ๊ณ  ๋ฌธ์„œ ํ™•์ธํ•˜๊ธฐ#

Go ์—์„œ๋Š” HTTP ์š”์ฒญ์„ ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ์š”? ๊ณ  ํŒจํ‚ค์ง€ ๋ฅผ ๋ชจ์•„๋†“์€ ์‚ฌ์ดํŠธ์— ๊ฐ€์„œ http ๋ฅผ ๊ฒ€์ƒ‰ํ•ด๋ด…๋‹ˆ๋‹ค. JAVA๋กœ ์น˜๋ฉด Maven Repository ๊ฐ™์€ ๊ณณ์ด๊ฒ ๋„ค์š”. ์˜ˆ์ œ๊ฐ€ ์นœ์ ˆํ•˜๊ฒŒ ์จ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์‹์ƒํ•œ ์ธ์‚ฌ๋Š” ๊ทธ๋งŒํ•˜๊ณ  ์•”ํ˜ธํ™”ํ์˜ ๋Œ€์žฅ์ธ ๋น„ํŠธ์ฝ”์ธ์— ๊ฐ€๊ฒฉ์„ ๊ฐ€์ง€๊ณ  ์™€๋ณผ๊ป˜์š”.

package main
import (
"io/ioutil"
"net/http"
)
func main() {
resp, err := http.Get("https://api.upbit.com/v1/ticker?markets=KRW-BTC")
if err != nil {
panic(err)
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
println(string(data))
}
[{"market":"KRW-BTC","trade_date":"20210815","trade_time":"084318","trade_date_kst":"20210815","trade_time_kst":"174318","trade_timestamp":1629016998000,"opening_price":54817000.00000000,"high_price":55166000.0,"low_price":53411000.0,"trade_price":53687000.0,"prev_closing_price":54812000.00000000,"change":"FALL","change_price":1125000.00000000,"change_rate":0.0205247026,"signed_change_price":-1125000.00000000,"signed_change_rate":-0.0205247026,"trade_volume":0.05957287,"acc_trade_price":239700491633.154070000,"acc_trade_price_24h":574757863109.85536000,"acc_trade_volume":4405.80968434,"acc_trade_volume_24h":10554.59700851,"highest_52_week_price":81994000.00000000,"highest_52_week_date":"2021-04-14","lowest_52_week_price":11860000.00000000,"lowest_52_week_date":"2020-09-07","timestamp":1629016998428}]

๋งˆ์น˜๋ฉฐ#

๊ฐ„๋‹จํ•œ ๋ช‡ ์ค„์— ์ฝ”๋“œ๋กœ ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์™”์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ค๋Š” ๋ฒ•์„ ์•Œ์•˜์œผ๋‹ˆ ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์—ฌ๊ธฐ์ €๊ธฐ์„œ ๊ฐ€์ง€๊ณ  ์™€์„œ ์š”๋ฆฌ์กฐ๋ฆฌ ๋š๋”ฑ๋š๋”ฑํ•˜๋ฉด ์›ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ์•ฑ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ฒ ์–ด์š”. ๊ทธ๋Ÿฐ๋ฐ... ์ € JSON ๋ฌธ์ž์—ด๋กœ ๋œ ๋†ˆ์„ ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ์š”? Go ์— ์ € ๋ฐ์ดํ„ฐ๋ฅผ ์ด์˜๊ฒŒ ๋‹ด์•„๋†“๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์„๊นŒ์š”? ๋‹ค์Œ ๋ฒˆ์—๋Š” ์ด ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.