douzhigan1687 2018-08-12 10:15 采纳率: 0%
浏览 132
已采纳

如何使用字符串。包含在接口上

I am receiving a panic on the following line of code. interface conversion: interface {} is []string, not string

My interface is a map of strings. What is the best method to find whether the figure interface contains a specific string.

if strings.Contains(figure["figure1"].(string), "one"){
}
  • 写回答

1条回答 默认 最新

  • dongyue934001 2018-08-12 10:45
    关注

    Here your type assertion is wrong, you are trying to access a value from a key on your interface, which hasn't been accessed as a map, so this can't work as the interface{} isn't indexible.

    What you have to do instead is to cast your whole interface to a map[string]string like such

    stringMap, ok := interfaceArg.(map[string]string)
    if !ok {
        return errors.New("cast failed")
    }
    
    if strings.Contains(stringMap["figure1"], "one") {
        // your code here
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分