Skip to content

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:

MTsocketAPI source 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))

}
  1. Install dependencies:

    sudo apt update
    sudo apt install golang-go
    
  2. Run:

    go run main.go
    

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))

}
  1. Install dependencies:

    sudo apt update
    sudo apt install golang-go
    
  2. Run:

    go run main.go
    

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:

  1. Initialize Go Modules (from your project directory)::

    go mod init your-module-name  # Example: go mod init websocket-client
    
  2. Install the WebSocket dependency:

    go get github.com/gorilla/websocket
    
  3. Run the program:

    go run websocket-client.go