在Golang中,是否可以定义一个类型取决于输入的变量?

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

Is it possible to define a variable which type is depends on input in Golang

问题

学习Go并尝试编写一个应用程序,该应用程序以位深度作为输入。当bitDepth == 8时,定义一个变量var y []byte。当bitDepth == 10时,定义一个变量var y []uint16

在Go中应该如何正确实现这个功能?

英文:

Learning Go and try to write an App which take bit depth as input, when bitDepth == 8, define a varable as var y []byte, When bitDepth == 10, define a variable var y []uint16

What's the right way to do it in Go?

答案1

得分: 1

因为Go语言中没有泛型,如果你使用空接口interface{},仍然需要进行类型断言。

最好的选择是定义一个接口,提供你所需的所有功能,并为需要包装的所有数据类型实现该接口。

package main

type SliceWrapper interface {
    Slice(start, end int) SliceWrapper
    Index(index int) int
}

func NewSlice(bitDepth int) SliceWrapper {
    switch bitDepth {
    case 8:
        return make(Uint8Wrapper)
    case 16:
        return make(Uint16Wrapper)
    }
}

type Uint8Wrapper []uint8

func (u Uint8Wrapper) Slice(s, e int) SliceWrapper {
    return u[s:e]
}

func (u Uint8Wrapper) Index(i int) int {
    return u[i]
}

type Uint16Wrapper []uint16

func (u Uint16Wrapper) Slice(s, e int) SliceWrapper {
    return u[s:e]
}

func (u Uint16Wrapper) Index(i int) int {
    return u[i]
}

我确定你需要更多的功能,但这比到处使用interface{}要干净得多。

英文:

Because there are no generics in go, you will still have to do type assertions if you use an empty interface i.e. interface{}.

The best option is to define an interface that provides all of the functionality you need, and implement it for all the datatypes you need to wrap.

package main

type SliceWrapper interface {
	Slice(start, end int) SliceWrapper
	Index(index int) int
}

func NewSlice(bitDepth int) SliceWrapper {
	switch bitDepth {
	case 8:
		return make(Uint8Wrapper)
	case 16:
		return make(Uint16Wrapper)
	}
}

type Uint8Wrapper []uint8

func (u Uint8Wrapper) Slice(s, e int) SliceWrapper {
	return u[s:e]
}

func (u Uint8Wrapper) Index(i int) int {
	return u[i]
}

type Uint16Wrapper []uint16

func (u Uint16Wrapper) Slice(s, e int) SliceWrapper {
	return u[s:e]
}

func (u Uint16Wrapper) Index(i int) int {
	return u[i]
}

You will need more functionality than that I am sure, but it is way cleaner than throwing interface{} around everywhere.

答案2

得分: 0

一种方法是:

package main

import (
	"fmt"
)

func virtualVar(bitDepth int) interface{} {
	var v interface{}
	switch bitDepth {
	case 8:
		y := []byte{1, 2}
		v = y
	case 16:
		y := []uint16{12345, 11000}
		v = y
	}
	return v
}

func main() {
	a := virtualVar(8)
	b := virtualVar(16)
	fmt.Println(a) //[1 2]
	fmt.Println(b) //[12345 11000]
}
英文:

one way is:

package main

import (
	"fmt"
)

func virtualVar(bitDepth int) interface{} {
	var v interface{}
	switch bitDepth {
	case 8:
		y := []byte{1, 2}
		v = y
	case 16:
		y := []uint16{12345, 11000}
		v = y
	}
	return v
}

func main() {
	a := virtualVar(8)
	b := virtualVar(16)
	fmt.Println(a) //[1 2]
	fmt.Println(b) //[12345 11000]
}

huangapple
  • 本文由 发表于 2016年4月30日 11:32:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/36950395.html
匿名

发表评论

匿名网友

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

确定