github.com/zignig/go-ipfs@v0.0.0-20141111235910-c9e5fdf55a52/core/commands2/internal/slice_util.go (about)

     1  package internal
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  )
     7  
     8  var CastErr = errors.New("cast error")
     9  
    10  func CastToReaders(slice []interface{}) ([]io.Reader, error) {
    11  	readers := make([]io.Reader, 0)
    12  	for _, arg := range slice {
    13  		reader, ok := arg.(io.Reader)
    14  		if !ok {
    15  			return nil, CastErr
    16  		}
    17  		readers = append(readers, reader)
    18  	}
    19  	return readers, nil
    20  }
    21  
    22  func CastToStrings(slice []interface{}) ([]string, error) {
    23  	strs := make([]string, 0)
    24  	for _, maybe := range slice {
    25  		str, ok := maybe.(string)
    26  		if !ok {
    27  			return nil, CastErr
    28  		}
    29  		strs = append(strs, str)
    30  	}
    31  	return strs, nil
    32  }