پرش به مطلب اصلی

نمونه کد Go

زبان Go به دلیل پرفورمنس بالا برای سرویس‌های میکروسرویس بسیار محبوب است. در اینجا از پکیج‌های استاندارد net/http استفاده می‌کنیم.

اسکریپت ارسال

main.go
package main

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)

// تعریف ساختار درخواست
type RequestBody struct {
Token string `json:"token"`
Destination string `json:"destination"`
Action string `json:"action"`
Payload map[string]interface{} `json:"payload"`
}

func main() {
url := "[https://gateway.ferzz.ir/send](https://gateway.ferzz.ir/send)"

// آماده‌سازی داده‌ها
requestData := RequestBody{
Token: "YOUR_ACCESS_TOKEN",
Destination: "989123456789",
Action: "flashcall",
Payload: map[string]interface{}{
"code": "12345",
},
}

// تبدیل به JSON
jsonData, _ := json.Marshal(requestData)

// ساخت ریکوئست
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
fmt.Println("Error creating request:", err)
return
}
req.Header.Set("Content-Type", "application/json")

// ارسال درخواست
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error sending request:", err)
return
}
defer resp.Body.Close()

// خواندن پاسخ
body, _ := ioutil.ReadAll(resp.Body)

if resp.StatusCode == 200 {
fmt.Println("✅ Success:", string(body))
} else {
fmt.Println("❌ Error:", resp.Status, string(body))
}
}