我刚开始学习Go lang,我写了一个小演示,从txt读取图片网址,将网址放入数组,然后将响应保存到文件中.
这是我的代码
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
)
func main() {
fileName := "meinv.txt"
file, _ := os.Open(fileName)
picUrl := make([]string, 2000)
reader := bufio.NewReader(file)
for {
line, _, err := reader.ReadLine()
if err != io.EOF {
fmt.Printf("file load %s \n", line)
picUrl = append(picUrl, string(line))
} else {
file.Close()
break
}
}
fmt.Printf("file loaded,read to download \n")
fetchPic(picUrl)
}
func fetchPic(picUrl []string) {
var file string
for key, value := range picUrl { …Run Code Online (Sandbox Code Playgroud) 我试图在Golang Web应用程序中使用mysqlstore后端程序包进行gorilla会话.我正在关注这个例子,我的代码是相同的.代码构建和运行正常,但当我去localhost:8080 /在我的浏览器中我收到此错误
运行时错误:无效的内存地址或无指针取消引用
这是我的代码:
package main
import (
"fmt"
"github.com/srinathgs/mysqlstore"
"net/http"
)
var store *mysqlstore.MySQLStore
func sessTest(w http.ResponseWriter, r *http.Request) {
session, err := store.Get(r, "foobar")
session.Values["bar"] = "baz"
session.Values["baz"] = "foo"
err = session.Save(r, w)
fmt.Printf("%#v\n", session)
fmt.Println(err)
}
func main() {
store, err := mysqlstore.NewMySQLStore("root:mypass@tcp(127.0.0.1:3306)/mydb?parseTime=true&loc=Local", "sessions", "/", 3600, []byte("<SecretKey>"))
if err != nil {
panic(err)
}
defer store.Close()
http.HandleFunc("/", sessTest)
http.ListenAndServe(":8080", nil)
}
Run Code Online (Sandbox Code Playgroud)
这是完整的错误消息:
2019/02/12 02:46:43 http:panic serving [:: 1]:63119:运行时错误:无效的内存地址或无指针取消引用goroutine 34 [运行]:net/http.(*conn).serve.func1 (0xc000112320)/usr/local/Cellar/go/1.11.5/libexec/src/net/http/server.go:1746 + 0xd0 …
go ×2