<p><code>
{
"type": "subscribe",
"product_ids": [
"ETH-USD",
"ETH-EUR"
],
"channels": [
"level2",
"heartbeat",
{
"name": "ticker",
"product_ids": [
"ETH-BTC",
"ETH-GBP"
]
},
]
}
</code>
Whats going on with the channels there?
jsonlint says its garbage too...</p>
<p><a href="https://docs.gdax.com/?php#overview" rel="nofollow">https://docs.gdax.com/?php#overview</a></p>
<p>I know this isn't strictly a go question, but I'm writing the API in go and I like you guys.</p>
<ul>
<li>not sure why its formating like garbage</li>
</ul>
<hr/>**评论:**<br/><br/>gjs278: <pre><pre><code> go func() {
for {
var wsDialer websocket.Dialer
wsConn, _, err := wsDialer.Dial(structs.GDAXExchange.Socket, nil)
if err != nil {
println(err.Error())
continue
}
if err := wsConn.WriteJSON(map[string]string{"type": "subscribe", "product_id": "BTC-USD"}); err != nil {
println(err.Error())
wsConn.Close()
continue
}
if err = wsConn.WriteJSON(map[string]string{"type": "subscribe", "product_id": "ETH-USD"}); err != nil {
println(err.Error())
wsConn.Close()
continue
}
if err = wsConn.WriteJSON(map[string]string{"type": "subscribe", "product_id": "LTC-USD"}); err != nil {
println(err.Error())
wsConn.Close()
continue
}
message := Message{}
for {
if err := wsConn.ReadJSON(&message); err != nil {
println(err.Error())
break
}
if message.Type == "match" {
switch message.ProductId {
case "BTC-USD":
structs.GDAXExchange.Coins["BTC"].Price = message.Price
structs.GDAXExchange.Coins["BTC"].Buy = structs.GDAXExchange.Coins["BTC"].Price
structs.GDAXExchange.Coins["BTC"].Sell = structs.GDAXExchange.Coins["BTC"].Price
case "ETH-USD":
structs.GDAXExchange.Coins["ETH"].Price = message.Price
structs.GDAXExchange.Coins["ETH"].Buy = structs.GDAXExchange.Coins["ETH"].Price
structs.GDAXExchange.Coins["ETH"].Sell = structs.GDAXExchange.Coins["ETH"].Price
case "LTC-USD":
structs.GDAXExchange.Coins["LTC"].Price = message.Price
structs.GDAXExchange.Coins["LTC"].Buy = structs.GDAXExchange.Coins["LTC"].Price
structs.GDAXExchange.Coins["LTC"].Sell = structs.GDAXExchange.Coins["LTC"].Price
}
}
}
wsConn.Close()
}
}()
</code></pre></pre>twek: <pre><p>That's a pretty simple example thanks for that</p></pre>deapen: <pre><p>Yes, it is valid JSON: <a href="http://json.org/" rel="nofollow">http://json.org/</a>. Arrays can have any value and the values do not need to be the same type.</p></pre>Kraigius: <pre><p>This is actually not a valid json. I have highlighted the error:</p>
<pre><code>{
"type": "subscribe",
"product_ids": [ "ETH-USD", "ETH-EUR" ],
"channels": [
"level2",
"heartbeat",
{
"name": "ticker",
"product_ids": [ "ETH-BTC", "ETH-GBP" ]
},<-- Error
]
}
</code></pre>
<p><em>edit</em>: Enhanced highlight</p></pre>twek: <pre><p>wow, will the go json package do this? I guess an array of interfaces...?</p></pre>SilentWeaponQuietWar: <pre><p>You can, but for instances like this I typically end up using something like <a href="https://github.com/buger/jsonparser" rel="nofollow">https://github.com/buger/jsonparser</a></p></pre>dilap: <pre><p>another approach (using the std json library) would be to make a type that contains pointers to possible type of entry in the array, and has a custom UnmarshalJSON method. the UnmarshalJSON method would do just enough custom introspection to figure out what kind of element it was looking at, and then unmarshall into the appropriate entry.</p></pre>twek: <pre><p>I like the look of this library I'll try it out</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传