允许将任意类型的切片作为参数传入。

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

Allow a slice of any type into as argument

问题

我是你的中文翻译助手,以下是你提供的代码的翻译:

我刚开始学习Go语言(之前使用Python),在这里遇到了一些困难。我想要允许任何类型的切片进入我的结构体/函数,并且它只包含该切片的长度计数。

import "go/types"

type Response struct {
    Count   int              `json:"count"`
    Results []types.Struct   `json:"results"`
}

func NewResponse(results []types.Struct) (r *Response) {
    r.Count = len(results)
    r.Results = results
    return
}

希望对你有帮助!

英文:

I am new to Go (coming from python) and I am having a bit of a hard time here. I am trying to allow any type of slice into my struct/func and it just contains a count of the length of that slice.

import "go/types"

type Response struct {
	Count int `json:"count"`
	Results []types.Struct `json:"results`
}

func NewResponse(results []types.Struct) (r *Response) {
	r.Count = len(results)
	r.Results = results
	return
}

答案1

得分: 1

你可以使用interface{}作为任何类型。

type Response struct {
  Count int `json:"count"`
  Results []interface{} `json:"results"`
}

更新

len(rsp.results)应该可以工作。
http://play.golang.org/p/RA2zVzWl2q

英文:

you can use interface{} as any type.

type Response struct {
  Count int `json:"count"`
  Results []interface{} `json:"results`
}

UPDATE

len(rsp.results) should work.
http://play.golang.org/p/RA2zVzWl2q

答案2

得分: 0

任意类型在Go语言中是完全合法的。在你的情况下,使用[]interface{}作为Results的类型可能是合适的。当需要知道类型时,可以使用类型切换

package main

import (
	"fmt"
)

type Response struct {
	Count   int           `json:"count"`
	Results []interface{} `json:"results"`
}

func NewResponse(results []interface{}) (r *Response) {
	r.Count = len(results)
	r.Results = results
	return
}

func AssertResultType(results []interface{}) {

	for _, v := range results {
		switch v := v.(type) {
		default:
			fmt.Printf("unexpected type %T\n", v) //t has unexpected type
		case bool:
			fmt.Printf("boolean %t\n", v) // t has type bool
		case int:
			fmt.Printf("integer %d\n", v) // t has type int
		case string:
			fmt.Printf("string %q\n", v) // t has type string
		}
	}

}

func main() {

	args := []interface{}{1, "hello", true, "foo", 21}    

    r := NewResponse(args)

    AssertResultType(r.Results)
}

在JSON的情况下,*json.RawMessage可以被编组为[]byte类型。

type Response struct {
	Count   int              `json:"count"`
	Results *json.RawMessage `json:"results"`
}
英文:

Arbitrary types are totally legit in Go. In your case, it might be appropriate to use []interface{} as the type of Results. When ever the types need to be known, use type switch.

package main

import (
	"fmt"
)

type Response struct {
	Count   int           `json:"count"`
	Results []interface{} `json:"results`
}

func NewResponse(results []interface{}) (r *Response) {
	r.Count = len(results)
	r.Results = results
	return
}

func AssertResultType(results []interface{}) {

	for _, v := range results {
		switch v := v.(type) {
		default:
			fmt.Printf("unexpected type %T\n", v) //t has unexpected type
		case bool:
			fmt.Printf("boolean %t\n", v) // t has type bool
		case int:
			fmt.Printf("integer %d\n", v) // t has type int
		case string:
			fmt.Printf("string %q\n", v) // t has type string
		}
	}

}

func main() {

	args := []interface{}{1, "hello", true, "foo", 21}    

    r := NewResponse(args)

    AssertResultType(r.Results)
}

In case of JSON, *json.RawMessage can be marshaled to type []byte

type Response struct {
	Count   int              `json:"count"`
	Results *json.RawMessage `json:"results`
}

huangapple
  • 本文由 发表于 2015年11月17日 14:03:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/33750179.html
匿名

发表评论

匿名网友

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

确定