title: Store username in array from login to server
body: <p>I have a small Go web-server that displays data to users as they login. The problem I'm trying to achieve is to have the web-page only show certain information when a specific user logs in. For example, when admin logs in there would be a list of admin-only items that they can see on the web-page.</p>
  
  <p>The problem I'm having is for some reason my Go code isn't storing the username in the array that I'm calling, so when I pass it over to the JavaScript it is blank. </p>
  
  <p>Here are the 3 main parts of the code that I'm struggling with:</p>
  
  <p><code>main.go</code></p>
  
  <pre><code>package main
  
  import "fmt"
  
  func authHandler(w http.ResponseWriter, r *http.Request) {
      r.ParseForm()
      usernameArray, hasUsername := r.PostForm["j_username"]
  
      //This line added for debugging purposes
      log.Println("username:", usernameArray[0])
  
      if hasUsername {
          fmt.Fprintf(w, "%s", usernameArray[0])
      }
  }
  
  func main() {
      http.HandleFunc("/getAuth", authHandler)
  }
  </code></pre>
  
  <hr>
  
  <p><code>javascript.js</code></p>
  
  <p>Note that this is AngularJS</p>
  
  <pre><code>$scope.init = function() {
      checkAuthentication();
  };
  
  checkAuthentication = function() {
      $http.get("/getAuth").then(
      function(response) {
          var username = response.data;
  
          console.log(username): //Added for debugging purposes
  
          if (username === "admin") {
              $scope.showAdminOnlyItems = true;
          }
      });
  }
  </code></pre>
  
  <hr>
  
  <p><code>main.html</code></p>
  
  <pre><code>&lt;div id="admin-only-items" ng-show="showAdminOnlyItems"&gt;
      &lt;hr style="border:1px solid"&gt;
      &lt;p style="text-align: center"&gt;admin only: &lt;/p&gt;
      &lt;div id="adminOnlyButtom"&gt;
          &lt;button class="button" data-ng-click="doSomething()"&gt;Do something&lt;/button&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Again, I only want that div to show up when admin logs in, and Go needs to send the username over to the javascript to verify that. By adding the debugging line in Go and starting the server up, I get this:</p>
  
  <pre><code>2018/11/19 16:28:42 http: panic serving 10.240.49.238:59621: runtime error: 
  index out of range
  goroutine 26 [running]:
  net/http.(*conn).serve.func1(0xc42009f720)
          C:/Go/src/net/http/server.go:1726 +0xd0
  panic(0x79f820, 0xe17940)
          C:/Go/src/runtime/panic.go:502 +0x229
  main.authHandler(0xc56220, 0xc420135180, 0xc4207a3500)
          D:/src/main.go:346 +0x1d8
  </code></pre>
  
  <p>So it's clear that the usernameArray is empty, not sure what I did wrong. Can anyone help tell me why the usernameArray is empty in authHandler?</p>