title: How to change pointer slice in golang
body: <p>I'm trying to get a better understanding of go. I created a little exercise for myself: pass a pointer slice to a function and modify it.</p>
  
  <p>This is what I came up with:</p>
  
  <pre><code>package main
  
  import (
      "fmt"
      "unsafe"
  )
  
  func main() {
      var data *[]byte
      fillData(data)
      fmt.Println((*data)[0:5])
  }
  
  func fillData(data *[]byte) {
      b := []byte("hello")
      fmt.Println(b[0:5])
      fmt.Println(string(b[0:5]))
      data = (*[]byte)(unsafe.Pointer(&amp;b[0]))
  }
  </code></pre>
  
  <p>But it gives an <code>invalid memory address or nil pointer dereference</code> error. I know I wouldn't use something like this in real code but I was just curious how to pass a slice and modify it without returning it.</p>
  
  <p><a href="https://play.golang.org/p/_K5ltKKKNV" rel="nofollow">https://play.golang.org/p/_K5ltKKKNV</a></p>