无法将 res.Data(类型为 []twitterstream.rulesResponseValue)转换为类型 []byte。

huangapple go评论83阅读模式
英文:

Cannot convert res.Data (type []twitterstream.rulesResponseValue) to type []byte

问题

我已经浪费了很多时间,试图将数据从这个函数传递到另一个函数,我真的在这一点上需要帮助。

type Rules struct {
	Value string `json:"value"`
	Tag   string `json:"tag"`
	Id    string `json:"id"`
}

func getRules() {
	accessKey := os.Getenv("API_KEY")
	accessSecret := os.Getenv("API_SECRET_KEY")

	tok, err := twitterstream.NewTokenGenerator().SetApiKeyAndSecret(accessKey, accessSecret).RequestBearerToken()
	if err != nil {
		panic(err)
	}
	api := twitterstream.NewTwitterStream(tok.AccessToken)
	res, err := api.Rules.GetRules()

	if err != nil {
		panic(err)
	}

	if res.Errors != nil && len(res.Errors) > 0 {
		panic(fmt.Sprintf("Received an error from twiiter: %v", res.Errors))
	}


	bytes := []byte(res.Data)
    // 这是关键行,这里需要处理

	var rules []Rules
	json.Unmarshal(bytes, &rules)

	for l := range rules {
		fmt.Printf("Id = %v, Value = %v", rules[l].Id, rules[l].Value)
		fmt.Println()
	}

	fmt.Println(res.Data)
}

我得到的错误是:
cannot convert res.Data (type []twitterstream.rulesResponseValue) to type []byte

有人可以帮我处理这个数据吗?我试图将数据转换为我自己的结构体,但一直遇到问题!

英文:

I've been wasting many hours trying to pass data from this function to another, and I really need help at this point.

type Rules struct {
	Value string `json:"value"`
	Tag   string `json:"tag"`
	Id    string `json:"id"`
}

func getRules() {
	accessKey := os.Getenv("API_KEY")
	accessSecret := os.Getenv("API_SECRET_KEY")

	tok, err := twitterstream.NewTokenGenerator().SetApiKeyAndSecret(accessKey, accessSecret).RequestBearerToken()
	if err != nil {
		panic(err)
	}
	api := twitterstream.NewTwitterStream(tok.AccessToken)
	res, err := api.Rules.GetRules()

	if err != nil {
		panic(err)
	}

	if res.Errors != nil && len(res.Errors) > 0 {
		panic(fmt.Sprintf("Received an error from twiiter: %v", res.Errors))
	}


	bytes := []byte(res.Data)
    // this is the line, here 


	var rules []Rules
	json.Unmarshal(bytes, &rules)

	for l := range rules {
		fmt.Printf("Id = %v, Value = %v", rules[l].Id, rules[l].Value)
		fmt.Println()
	}

	fmt.Println(res.Data)
}

The error I'm getting is:
cannot convert res.Data (type []twitterstream.rulesResponseValue) to type []byte

Can somebody help me handle this data? I'm trying to convert the data into my own struct that I can return but it's been one thing after another!

答案1

得分: 2

只要您的Rules类型与rulesResponseValue类型具有相同的底层类型,您可以在Data切片的各个元素上使用转换表达式,将rulesResponseValue类型的实例直接转换为Rules类型的实例。

// 我将Rules重命名为Rule
type Rule struct {
	Value string `json:"value"`
	Tag   string `json:"tag"`
	Id    string `json:"id"`
}

func getRules() ([]Rule, error) {
	accessKey := os.Getenv("API_KEY")
	accessSecret := os.Getenv("API_SECRET_KEY")

	tok, err := twitterstream.NewTokenGenerator().SetApiKeyAndSecret(accessKey, accessSecret).RequestBearerToken()
	if err != nil {
		return nil, err
	}
	api := twitterstream.NewTwitterStream(tok.AccessToken)
	res, err := api.Rules.GetRules()
	if err != nil {
		return nil, err
	} else if res.Errors != nil && len(res.Errors) > 0 {
		return nil, fmt.Errorf("Received an error from twitter: %v", res.Errors)
	}

	rules := make([]Rule, len(res.Data))
	for i := range res.Data {
		rules[i] = Rule(res.Data[i]) // 将rulesResponseValue转换为Rule
	}

	return rules, nil
}

然后您可以像这样使用它

func main() {
	rules, err := getRules()
	if err != nil {
		panic(err)
	}
	for _, r := range rules {
		fmt.Printf("Id = %v, Value = %v\n", r.Id, r.Value)
	} 
}
英文:

As long as your Rules type has the same underlying type as the rulesResponseValue type, you can use the conversion expression on the individual elements of the Data slice to directly convert an instance of type rulesResponseValue to an instance of the Rules type.

// I renamed Rules to Rule
type Rule struct {
	Value string `json:"value"`
	Tag   string `json:"tag"`
	Id    string `json:"id"`
}

func getRules() ([]Rule, error) {
	accessKey := os.Getenv("API_KEY")
	accessSecret := os.Getenv("API_SECRET_KEY")

	tok, err := twitterstream.NewTokenGenerator().SetApiKeyAndSecret(accessKey, accessSecret).RequestBearerToken()
	if err != nil {
		return nil, err
	}
	api := twitterstream.NewTwitterStream(tok.AccessToken)
	res, err := api.Rules.GetRules()
	if err != nil {
		return nil, err
	} else if res.Errors != nil && len(res.Errors) > 0 {
		return nil, fmt.Errorf("Received an error from twitter: %v", res.Errors)
	}

	rules := make([]Rule, len(res.Data))
	for i := range res.Data {
		rules[i] = Rule(res.Data[i]) // convert rulesResponseValue to Rule
	}

	return rules, nil
}

And you can then use that like this

func main() {
	rules, err := getRules()
	if err != nil {
		panic(err)
	}
	for _, r := range rules {
		fmt.Printf("Id = %v, Value = %v\n", r.Id, r.Value)
	} 
}

huangapple
  • 本文由 发表于 2021年7月31日 09:55:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/68598507.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定