dream8877 2016-07-28 08:40
浏览 268
已采纳

如何在GO中的多返回函数中仅返回一个变量?

Hi I have this function which I want to return a docker.APIContainer and an error (nil if no errors), but in case of error what should I return in docker.APIContainer ?

this is the code

func GetContainersRunningImage(imagename string, tag string) ([]docker.Container,string) {
   logFields := log.Fields{

       "handler": "get service",
   }
   client, err := docker.NewTLSClient(sconf.DockConf.Endpoint, sconf.DockConf.Cert, sconf.DockConf.Key, sconf.DockConf.Ca)
   if err != nil {
      log.WithFields(logFields).Errorf("TLSClient could not be created: %s", err)
      return _,err.Error()
   }
}

what should I add instead of _? Am I obliged to create a var contarray []docker.Container and return it? or is there another way

  • 写回答

2条回答 默认 最新

  • down123321123 2016-07-28 08:47
    关注
    return nil, err.Error()
    

    goreturns will do this for you automatically - 0 for int and "" for string etc.

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

报告相同问题?

悬赏问题

  • ¥15 vs2010修改时间编辑控件
  • ¥15 郑州牧原测试岗值得去吗
  • ¥100 复现论文:matlab仿真代码编写
  • ¥15 esp32驱动GC9A01循环播放视频
  • ¥15 惠普360g9的最新bios
  • ¥30 这个功能用什么软件发合适?
  • ¥60 微信小程序,取消订单,偶尔订单没有改变状态
  • ¥15 用pytorch实现PPO算法
  • ¥15 关于调制信号的星座图?
  • ¥30 前端传参时,后端接收不到参数