You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Split Go SDK中读取Treatment的sk_price参数值

获取Split.io Feature Flag的Treatment参数

你当前的代码把client.Treatment的返回值直接当作字符串处理,但实际上这个方法返回的是client.TreatmentResult结构体,里面同时包含treatment状态和对应的配置参数。要获取sk_price,需要解析结构体里的Config字段。

修改后的代码如下:

import (
    "encoding/json"
    "fmt"
    "github.com/splitio/go-client/v6/splitio/client"
    "github.com/splitio/go-client/v6/splitio/conf"
)

// 定义结构体映射配置参数
type SkyConfig struct {
    SkPrice string `json:"sk_price"`
}

func main() {
    cfg := conf.Default()
    factory, err := client.NewSplitFactory("YOUR_SDK_TYPE_API_KEY", cfg)
    if err != nil {
        fmt.Printf("创建Split工厂失败: %v\n", err)
        return
    }

    splitClient := factory.Client()
    defer splitClient.Destroy() // 程序结束时销毁客户端,清理资源

    err = splitClient.BlockUntilReady(10)
    if err != nil {
        fmt.Printf("客户端超时未就绪: %v\n", err)
        return
    }

    // 获取完整的Treatment结果,而非单纯字符串
    treatmentResult := splitClient.Treatment("CUSTOMER_ID", "sky_name", nil)
    
    if treatmentResult.Name == "on" {
        var skyConfig SkyConfig
        // 解析Config字段到结构体
        err = json.Unmarshal([]byte(treatmentResult.Config), &skyConfig)
        if err != nil {
            fmt.Printf("解析配置失败: %v\n", err)
            return
        }
        // 获取sk_price的值
        fmt.Printf("sk_price的值为: %s\n", skyConfig.SkPrice)
    } else if treatmentResult.Name == "off" {
        // 处理off状态逻辑
    } else {
        // 处理control(默认)状态逻辑
    }
}

关键说明:

  • TreatmentResult结构体包含Name(对应on/off/control状态)和Config(存储参数的JSON字符串)两个核心字段。
  • 使用encoding/json包将Config解析到自定义结构体,能安全提取sk_price的值。
  • 调用Destroy()方法确保程序结束时清理客户端资源,避免内存泄漏。

内容的提问来源于stack exchange,提问作者Madhawa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 16:27:13