Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type cast custom types to base types

Tags:

go

How can I convert custom type to interface{} and then to base type (ex. uint8)?

I can't use direct cast like uint16(val.(Year)) because I may not know all custom types, but I can determinate base types (uint8, uint32,...) in runtime


There are many custom types (usually used as enums) based on numeric:

ex:

type Year  uint16
type Day   uint8
type Month uint8

and so on...

The question is about type casting from interface{} to base types:

package main

import "fmt"

type Year uint16

// ....
//Many others custom types based on uint8

func AsUint16(val interface{}) uint16 {
    return val.(uint16) //FAIL:  cannot convert val (type interface {}) to type uint16: need type assertion
}

func AsUint16_2(val interface{}) uint16 {
    return uint16(val) //FAIL:   cannot convert val (type interface {}) to type uint16: need type assertion
}

func main() {
    fmt.Println(AsUint16_2(Year(2015)))
}

http://play.golang.org/p/cyAnzQ90At

like image 206
Alex Bar Avatar asked Jun 08 '15 20:06

Alex Bar


People also ask

What is type type casting?

In Java, type casting is a method or process that converts a data type into another data type in both ways manually and automatically. The automatic conversion is done by the compiler and manual conversion performed by the programmer.

How do you type a cast?

Typecasting is making a variable of one type, such as an int, act like another type, a char, for one single operation. To typecast something, simply put the type of variable you want the actual variable to act as inside parentheses in front of the actual variable. (char)a will make 'a' function as a char.

What is type-casting?

Converting an expression of a given type into another type is known as type-casting. We have already seen some ways to type cast: Implicit conversions do not require any operator. They are automatically performed when a value is copied to a compatible type. For example:

What is type casting in C?

Type-casting is an important concept in general programming. It converts one type to another and whenever we need some other types for the expression type casting helps. The syntax for general type casting is pretty simple. just use that other type name as a function to convert that value.

What is the correct syntax for casting between two types?

The syntax for general type casting is pretty simple. just use that other type name as a function to convert that value. e.g. i := int (32.987) // casting to integer

What are the different types of casting operators?

The subsequent call to member result will produce either a run-time error or a unexpected result. In order to control these types of conversions between classes, we have four specific casting operators: dynamic_cast, reinterpret_cast, static_cast and const_cast.


2 Answers

You can accomplish this by using the reflect package:

package main

import "fmt"
import "reflect"

type Year uint16

func AsUint16(val interface{}) uint16 {
    ref := reflect.ValueOf(val)
    if ref.Kind() != reflect.Uint16 {
        return 0
    }
    return uint16(ref.Uint())
}

func main() {
    fmt.Println(AsUint16(Year(2015)))
}

Depending on your situation, you may want to return (uint16, error), instead of returning the empty value.

https://play.golang.org/p/sYm1jTCMIf

like image 102
Tim Cooper Avatar answered Sep 28 '22 05:09

Tim Cooper


Why did you include Year in the question? Are you hoping to convert arbitrary things into Years, or convert Years into uint16s?

If I assume you meant the latter case, then it would be better to use a method

func (y Year) AsUint16() uint16 { 
    return uint16(y)
}

which doesn't need any type assertions or reflection.

https://play.golang.org/p/9wCQJe46PU

like image 25
Rick-777 Avatar answered Sep 28 '22 07:09

Rick-777