背景:
请求第三方服务,对方返回来某个字段,比如叫type,值为从1-9中的某个数字;我在这边接到这个返回值后,需要将1-5分别映射成某个字符串返回给前端,其余的数字都返回空。
最惨不忍睹的方法就是判断,if这个值等于几,返回啥啥啥;最后的else返回空;
在PHP中可以定义一个类似map:

    const conf = array(
        2 => 'ha',
        3 => 'haha',
        4 => 'hahaha',
        5 => 'hahahaha',
        6 => 'hahahahaha'
    );
    $res = self::conf[$xxx] ?? '';

在go中,首先是没有三元表达式的,那就得这样:

var (
   Conf = map[int]string{
   	2: "ha",
   	3: "haha",
   	4: "hahaha",
   	5: "hahahaha",
   	6: "hahahahaha",
   }
)
   tmp, ok := Conf[xxx]
   res := ""
   if ok {
   	res = tmp
   }

用这个is-ok模式来判断