dousi6087 2016-12-09 15:53
浏览 8
已采纳

去地图有重复的钥匙

I'm working on a small Go program that receives an ascii message over UDP. I want to look up the first field in the message and see if it exist in a map. Go thinks the key does not exist in the map but it does. I can add the key to the map and it creates a new entry, so I have two entries with the same key. I'm I doing something wrong or is this a bug?

EDIT: I've simplified the test down to remove the UDP and YAML.

https://play.golang.org/p/2Bg8UjhfWC

package main

import (
    "fmt"
    "strings"
)

type TestCase struct {
    Test string
    Result string
}

func main() {
    tcmap := make(map[string]TestCase)
    tcmap["adc"] = TestCase{Test: "/bar"}
    fmt.Printf("TestMap: ----------
%v

", tcmap)

    buf := make([]byte, 1024)
    buf[0] = 'a'//0x61
    buf[1] = 'd'//0x64
    buf[2] = 'c'//0x63

    fmt.Printf("Received: ---------
%v

", string(buf[0:3]))
    fmt.Printf("Compare hex:-------
|%x| |%x|

", buf[0:3], "adc")

    // Get the first field from the message
    testname := strings.Split(strings.Trim(string(buf), " "), " ")[0]
    fmt.Printf("Test Name: |%v|
", testname)

    // Does the key exist in the map?
    if t, ok := tcmap[testname]; ok {
        fmt.Printf("Test found: %v
", t)
    } else {
        fmt.Printf("Test NOT found
")
    }

    // Add testname to map, does it replace existing?
    tcmap[testname] = TestCase{Test: "/foo"}
    fmt.Printf("
MAP: ---------
%v

", tcmap)
    fmt.Printf("KEY adc:---------
%v

", tcmap["adc"])
    for k,v := range tcmap {
        fmt.Printf("%v: %v
", k, v)
    }
}

Output:

TestMap: ----------
map[adc:{/bar }]

Received: ---------
adc

Compare hex:-------
|616463| |616463|

Test Name: |adc|
Test NOT found

MAP: ---------
map[adc:{/bar } adc:{/foo }]

KEY adc:---------
{/bar }

adc: {/bar }
adc: {/foo }
  • 写回答

2条回答 默认 最新

  • dongqiaogouk86049 2016-12-12 14:00
    关注

    As pointed out by Alexander, the issue is the length between the two keys are different. One key has the length of 3 and the other has the length of 1024. The first three bytes were the same and on the longer key the remaining bytes were 0x00.

    So the string output of the two keys make it appear the two are identical, but this was fooling me. The length of the keys was different.

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

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line