1

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

3
  • @Jakumi I tried to return nil in an int type just trying to see if I can return it in all types but I got an error: cannot use nil as type int in return argument So I thought that it will give the same error in types other than string Commented Jul 28, 2016 at 8:48
  • @Jakumi just for testing I created a structure in go playground and tried to return a nil value I got an error play.golang.org/p/RytEExB6bv Commented Jul 28, 2016 at 8:54
  • 1
    @diegosouza Returning nil is best practice for a pointer return value (and can be used only for pointer values). For concrete types its up to you, it can be some meaningful value like in io.Readers Read method, or some "incorrect" value like 0 or -1 for int return value etc. See play.golang.org/p/70ilkDhcQc Commented Jul 28, 2016 at 9:02

2 Answers 2

1

GetContainersRunningImage returns multiple values: a []docker.Container and a string, which means you have to return two values with exactly the same type signatures, or nil in case you do not want to return a concrete value.

Because you are handling the error case it's ok to return a nil value in case of an error:

if err != nil {
      log.WithFields(logFields).Errorf("TLSClient could not be created: %s", err)
      return nil, err.Error()
}

But you have to return a value at the end of the function in case you don't receive an error. In this case the type signature of the return function should be of type: []docker.Container, string.

That's why in Go the idiomatic way of a function return argument is the value and an error if an error has occurred. Because you are handling the error separately in an if statement which returns two values: nil instead of the value and the error type, the final return should be the type value and nil for error.

func GetContainersRunningImage(imagename string, tag string) ([]docker.Container,string) {
   var contarray []docker.Container

   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 nil, err.Error()
   }

   return contarray, nil
}

Other possibility is to use the _(underscore) variable to disregard one of the return values, but this needs to happen on variable assignment.

Sign up to request clarification or add additional context in comments.

1 Comment

just for testing I created a structure in go playground and tried to return a nil value I got an error play.golang.org/p/RytEExB6bv
0
return nil, err.Error()

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

2 Comments

just for testing I created a structure in go playground and tried to return a nil value I got an error play.golang.org/p/RytEExB6bv
You are misunderstand how to properly return a nil value. Types in Go have different nil value - 0 for int, "" for string, nil for pointers... Now to your code on playground, there are 2 options: play.golang.org/p/tz_is19Lvn or play.golang.org/p/70ilkDhcQc. For the first one, you expect to return a nil struct, this is not possible in Go, so we need to return a struct with no data Fs{}. On the other hand, pointer to a struct can be nil.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.