title: Getting `panic: os: invalid use of WriteAt on file opened with O_APPEND`
body: <p>I am a newbie to Go. Was starting to write my first code in which I have to download a bunch of CSV's from AWS. I don't understand why it is giving me the below error with O_APPEND mode. If I remove <code>os.O_APPEND</code>, I only get the last file data which is not the objective.</p>
  <p>The objective is to download all CSV files into one file locally. I'd like to understand what I'm doing incorrectly.</p>
  <pre><code>package main
  
  import (
      &quot;fmt&quot;
      &quot;os&quot;
      &quot;path/filepath&quot;
  
      &quot;github.com/aws/aws-sdk-go/aws&quot;
      &quot;github.com/aws/aws-sdk-go/aws/credentials&quot;
      &quot;github.com/aws/aws-sdk-go/aws/session&quot;
      &quot;github.com/aws/aws-sdk-go/service/s3&quot;
      &quot;github.com/aws/aws-sdk-go/service/s3/s3manager&quot;
  )
  
  const (
      AccessKeyId     = &quot;xxxxxxxxx&quot;
      SecretAccessKey = &quot;xxxxxxxxxxxxxxxxxxxx&quot;
      Region          = &quot;eu-central-1&quot;
      Bucket          = &quot;dexter-reports&quot;
      bucketKey       = &quot;Jenkins/pluginVersions/&quot;
  )
  
  func main() {
      // Load the Shared AWS Configuration
      os.Setenv(&quot;AWS_ACCESS_KEY_ID&quot;, AccessKeyId)
      os.Setenv(&quot;AWS_SECRET_ACCESS_KEY&quot;, SecretAccessKey)
      filename := &quot;JenkinsPluginDetais.txt&quot;
  
      cred := credentials.NewStaticCredentials(AccessKeyId, SecretAccessKey, &quot;&quot;)
      config := aws.Config{Credentials: cred, Region: aws.String(Region), Endpoint: aws.String(&quot;s3.amazonaws.com&quot;)}
  
      file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666)
  
      if err != nil {
          panic(err)
      }
  
      defer file.Close()
  
      sess, err := session.NewSession(&amp;config)
  
      if err != nil {
          fmt.Println(err)
      }
  
      //list Buckets
      ObjectList := listBucketObjects(sess)
  
      //loop over the obectlist. First initialize the s3 downloader via s3manager
      downloader := s3manager.NewDownloader(sess)
  
      for _, item := range ObjectList.Contents {
          csvFile := filepath.Base(*item.Key)
  
          if csvFile != &quot;pluginVersions&quot; {
  
              downloadBucketObjects(downloader, file, csvFile)
  
          }
  
      }
  
  }
  
  func listBucketObjects(sess *session.Session) *s3.ListObjectsV2Output {
  
      //create a new s3 client
      svc := s3.New(sess)
      resp, err := svc.ListObjectsV2(&amp;s3.ListObjectsV2Input{
          Bucket: aws.String(Bucket),
          Prefix: aws.String(bucketKey),
      })
  
      if err != nil {
          panic(err)
      }
  
      return resp
  
  }
  
  func downloadBucketObjects(downloader *s3manager.Downloader, file *os.File, keyobj string) {
  
      fileToDownload := bucketKey + keyobj
  
      numBytes, err := downloader.Download(file,
          &amp;s3.GetObjectInput{
              Bucket: aws.String(Bucket),
              Key:    aws.String(fileToDownload),
          })
  
      if err != nil {
          panic(err)
      }
  
      fmt.Println(&quot;Downloaded&quot;, file.Name(), numBytes, &quot;bytes&quot;)
  
  }
  </code></pre>