doutizha7526 2018-01-14 19:38
浏览 99
已采纳

结构的唯一哈希

I would like to generate a unique hash from an array of structs. The order might be different but the values are the same.

Example:

type MyStruct struct {
   ID string
   Parameters map[string]interface{}
}

arr:= []MyStruct{MyStruct{ID: "test", Parameters: map[string]interface{}{"key": true, "key2": "value"} }, MyStruct{ID: "test2", Parameters: map[string]interface{}{"key2": "value", "key": true} }}
//The order is different even inside the Parameters fields

arr:= []MyStruct{MyStruct{ID: "test2", Parameters: map[string]interface{}{"key2": "value", "key": true} },MyStruct{ID: "test", Parameters: map[string]interface{}{"key": true, "key2": "value"} }}

In both cases the hash should be the same since the values inside the structs are the same.

EDIT: Basically the idea is to generate a unique hash for caching! I want to combine the individual hash of each struct!

  • 写回答

3条回答 默认 最新

  • dongren7374 2018-01-15 11:55
    关注

    You can do this by serializing the array to an array of bytes, then computing the md5 sum of the byte array. Since the md5 sum is the hashed sum of the bytes, the order will not matter.

    To serialize each element of the array, you can use json.Marshal, which will work for any type of struct.

    The hash function will look something like this :

    func Hash(arr []SomeStruct) [16]byte {
        arrBytes := []byte{}
        for _, item := range arr {
            jsonBytes, _ := json.Marshal(item)
            arrBytes = append(arrBytes, jsonBytes...)
        }
        return md5.Sum(arrBytes)
    }
    

    You can run the working sample program here

    func main() {
        arr1 := []SomeStruct{
            {
                param1: "abc",
                param2: 3,
            },
            {
                param1: "def",
                param2: 5,
            }, {
                param1: "deg",
                param2: 0,
            },
        }
        arr2 := []SomeStruct{
        {
                param1: "deg",
                param2: 0,
            },
            {
                param1: "def",
                param2: 5,
            },
            {
                param1: "abc",
                param2: 3,
            },
        }
    
        fmt.Printf("Hash1: %x
    ", Hash(arr1))
        fmt.Printf("Hash2: %x
    ", Hash(arr2))
    
    }
    
    func Hash(arr []SomeStruct) [16]byte {
        arrBytes := []byte{}
        for _, item := range arr {
            jsonBytes, _ := json.Marshal(item)
            arrBytes = append(arrBytes, jsonBytes...)
        }
        return md5.Sum(arrBytes)
    }
    

    Which will output:

    Hash1: d065fee603fdcf75115204ec65310e1c
    Hash2: d065fee603fdcf75115204ec65310e1c
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算