Go
Important
All these source codes are only examples used for testing. We don't provide any guarantee or responsibility about it. Use these examples at your own risk.
Generate source code instantly
Select your desired programming language via our web interface to automatically retrieve code:
Example 1: Get EURUSD price
main.go
package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "http://127.0.0.1:81/v1/quote?symbol=EURUSD"
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Accept", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(res)
fmt.Println(string(body))
}
-
Install dependencies:
-
Run:
Example 2: Send buy order
main.go
package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "http://127.0.0.1:81/v1/order?symbol=EURUSD&volume=0.01&type=ORDER_TYPE_BUY"
req, _ := http.NewRequest("POST", url, nil)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(res)
fmt.Println(string(body))
}
-
Install dependencies:
-
Run:
Example 3: Stream EURUSD price using websockets
websocket-client.go
package main
import (
"fmt"
"log"
"net/http"
"io"
"github.com/gorilla/websocket"
)
func main() {
url := "http://127.0.0.1:81/v1/track/prices?symbols=EURUSD"
req, _ := http.NewRequest("POST", url, nil)
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(res)
fmt.Println(string(body))
// Websockets
conn, _, err := websocket.DefaultDialer.Dial("ws://127.0.0.1:81", nil)
if err != nil {
log.Fatal(err)
}
defer conn.Close()
for {
_, message, err := conn.ReadMessage()
if err != nil {
log.Fatal(err)
}
fmt.Println("Received:", string(message))
}
}
To use this code:
-
Initialize Go Modules (from your project directory)::
-
Install the WebSocket dependency:
-
Run the program: