doutui8842 2013-10-11 18:45
浏览 16
已采纳

Golang,Go :(重新发布)Go中的通用和

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

I am so sorry. I accidentally deleted my previous question. Here is my second try.

I know that Go does not support generic types but there should be a way to do this.

I am trying to add any type of two arguments and return the result using interface and type assertion. But I am stuck at

  1. (+) is not defined in interface

  2. can't think of what type I should return

This is my previous step.

   func Add(val1, val2 interface{}) int {
        new_a := val1.(int)
        new_b := val2.(int)
        return new_a + new_b
   }

This give me the right answer but this is useless since I know the integer values will be passed. I want a function that does not know what would be given and return the addition accordingly to the given variable types.

Here is my second try and get stuck.

http://play.golang.org/p/-_jvvs09nl

 func Add(val1, val2 interface{}) {

// var x interface{} = 7  // x has dynamic type int and value 7
// i := x.(int)           // i has type int and value 7

// a := reflect.ValueOf(val1)
// b := reflect.ValueOf(val2)
// fmt.Println(a, b)
// <int Value> <int Value>

type_val1 := reflect.TypeOf(val1)
type_val2 := reflect.TypeOf(val2)
fmt.Println(type_val1, type_val2)

result1 := val1.(type_val1) // ERROR : type_val1 is not a type
result2 := val2.(type_val2) // ERROR : type_val2 is not a type
fmt.Println(result1, result2)

Thanks always.

  • 写回答

2条回答 默认 最新

  • douzhuang6321 2013-10-11 18:46
    关注

    In go there is no operator overloading. You can't define your own +.

    As you probably have seen already, there is also no method Add() defined on reflect.Value. To use + you have to get to the underlying type of the value and then add it. You can do this by using type assertions.

    According to the spec + is defined for integers, floats, complex values and strings. So you need to check whether the value is one of these types, convert it to that type and then add it.

    Example on play:

    func Add(a, b interface{}) (interface{}, error) {
        value_a := reflect.ValueOf(a)
        value_b := reflect.ValueOf(b)
    
        if value_a.Kind() != value_b.Kind() {
            return nil, fmt.Errorf("Different kinds, can't add them.")
        }
    
        switch value_a.Kind() {
            case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
                return value_a.Int() + value_b.Int(), nil
            case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
                return value_a.Uint() + value_b.Uint(), nil
            case reflect.Float32, reflect.Float64:
                return value_a.Float() + value_b.Float(), nil
            case reflect.String:
                return value_a.String() + value_b.String(), nil
            default:
                return nil, fmt.Errorf("Type does not support addition.")
        }
    }
    

    Note that the return type is interface{} as there may be different return types.

    This can, as with this related question, be optimized by using reflect.MakeFunc (example on play):

    func main() {
        var addInt func(int, int) int64
        var addFloat func(float32, float32) float64
    
        makeFunc(AddFunc, &addInt)
        makeFunc(AddFunc, &addFloat)
    
        fmt.Println( addInt(1, 1) )
        fmt.Println( addFloat(1.0, 3.1415) )
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算