donglin1692 2018-03-24 09:48
浏览 81


I am new on GOlang. So, I am inserting the data from an html page to mongodb database. But there is an error in the code. The code is below:-

package main

import (
type USER struct {
    Username string      `bson:"Username" json:"Username,omitempty"`
    Password string      `bson:"Password" json:"Password,omitempty"`
func sayhelloName(w http.ResponseWriter, r *http.Request) {
   for k, v := range r.Form {
      fmt.Println("key:", k)
      fmt.Println("val:", strings.Join(v, ""))
    fmt.Fprintf(w, "Hello astaxie!")
func login(w http.ResponseWriter, r *http.Request) {
   fmt.Println("method:", r.Method)
   if r.Method == "GET" {
       t, _ := template.ParseFiles("index.html")
       t.Execute(w, nil)
   } else {
         //connection with mongodb
        session, err := mgo.Dial("mongodb://")
        if err != nil {
    defer session.Close()
    session.SetMode(mgo.Monotonic, true)
    c := session.DB("so").C("insrt")
    doc := USER{
        Username: r.Form["username"][0],
        Password: r.Form["password"][0],
    err = c.Insert(doc)
    if err != nil {
", r.Form["username"])
    fmt.Println("username:", r.Form["username"][0])//output:- username: user_name
    fmt.Println("password:", r.Form["password"][0])//password:- password: user_password
func main() {
   http.HandleFunc("/", sayhelloName)
   http.HandleFunc("/login", login)
   err := http.ListenAndServe(":9090", nil)
   if err != nil {
      log.Fatal("ListenAndServe: ", err)


    <form action="/login" method="post">
        Username:<input type="text" name="username">
        Password:<input type="password" name="password">
        <input type="submit" value="Login">

From the above code the user will entered the data in the html form by accessing url localhost:9090/login when user click on submit button then the data will shown in the terminal I comment the mongodb code but when i want to save that data then it gives me error.

error:-2018/03/24 14:48:28 http: panic serving [::1]:44410: runtime error: index out of range goroutine 5 [running]: net/http.(*conn).serve.func1(0xc420094c80) /usr/local/go/src/net/http/server.go:1726 +0xd0 panic(0x78aa20, 0xa03de0) /usr/local/go/src/runtime/panic.go:505 +0x229 main.login(0x846c80, 0xc42015c0e0, 0xc42018a000) /home/iron/go/src/go-training/pagesweb/main.go:48 +0x767 net/http.HandlerFunc.ServeHTTP(0x8147d0, 0x846c80, 0xc42015c0e0, 0xc42018a000) /usr/local/go/src/net/http/server.go:1947 +0x44 net/http.(*ServeMux).ServeHTTP(0xa132c0, 0x846c80, 0xc42015c0e0, 0xc42018a000) /usr/local/go/src/net/http/server.go:2337 +0x130 net/http.serverHandler.ServeHTTP(0xc42008b2b0, 0x846c80, 0xc42015c0e0, 0xc42018a000) /usr/local/go/src/net/http/server.go:2694 +0xbc net/http.(*conn).serve(0xc420094c80, 0x847080, 0xc420146040) /usr/local/go/src/net/http/server.go:1830 +0x651 created by net/http.(*Server).Serve /usr/local/go/src/net/http/server.go:2795 +0x27b

Anybody can help me to solve my problem. I just want to save the data. in document of mongodb.

  • 写回答

1条回答 默认 最新

  • doutou6803 2018-03-24 10:17

    The docs clearly state (emphasis mine):

    This field [r.Form] is only available after ParseForm is called.

    You are accessing it before calling ParseForm.

    Also, r.Form is of type url.Values, so use r.Form.Get() to read the first value of a field.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 关于#单片机#的问题:以ATMEGA128或相近型号单片机为控制器设计直流电机调速的闭环控制系统(相关搜索:设计报告|软件设计|流程图)
  • ¥15 打开软件提示错误:failed to get wglChoosePixelFormatARB
  • ¥30 电脑误删了手机的照片怎么恢复?
  • ¥15 (标签-python|关键词-char)
  • ¥15 python+selenium,在新增时弹出了一个输入框
  • ¥15 苹果验机结果的api接口哪里有??单次调用1毛钱及以下。
  • ¥20 学生成绩管理系统设计
  • ¥15 来一个cc穿盾脚本开发者
  • ¥15 CST2023安装报错
  • ¥15 使用diffusionbert生成文字 结果是PAD和UNK怎么办