code: func DumpStacks(dir string) (string, error) {
  	var (
  		buf       []byte
  		stackSize int
  	)
  	bufferLen := 16384
  	for stackSize == len(buf) {
  		buf = make([]byte, bufferLen)
  		stackSize = runtime.Stack(buf, true)
  		bufferLen *= 2
  	}
  	buf = buf[:stackSize]
  	var f *os.File
  	if dir != "" {
  		path := filepath.Join(dir, fmt.Sprintf(stacksLogNameTemplate, strings.Replace(time.Now().Format(time.RFC3339), ":", "", -1)))
  		var err error
  		f, err = os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0666)
  		if err != nil {
  			return "", errors.Wrap(err, "failed to open file to write the goroutine stacks")
  		}
  		defer f.Close()
  		defer f.Sync()
  	} else {
  		f = os.Stderr
  	}
  	if _, err := f.Write(buf); err != nil {
  		return "", errors.Wrap(err, "failed to write goroutine stacks")
  	}
  	return f.Name(), nil
  }
docs: // DumpStacks appends the runtime stack into file in dir and returns full path
  // to that file.