Invalid memory address or nil pointer dereference

Hi everybody!

I’m new into programming and I’m having an issue with the “gowiki” web application example → https://golang.org/doc/articles/wiki/.

My code so far:

package main

import (
	"fmt"
	"io/ioutil"
)

//Page ...
type Page struct {
	Title string
	Body  []byte
}

func (p *Page) save() error {
	filename := p.Title + ".txt"
	return ioutil.WriteFile(filename, p.Body, 0600)
}

func loadPage(title string) (*Page, error) {
	filename := title + ".txt"
	body, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}
	return &Page{Title: title, Body: body}, nil
}

func main() {
	p1 := &Page{Title: "TestPage", Body: []byte("This is a sample Page.")}
	p1.save()
	p2, _ := loadPage("TestPage")
	fmt.Println(string(p2.Body))
}

I’m getting this error:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x40146a]

goroutine 1 [running]:
panic(0x4e7680, 0xc82000a110)
	/usr/lib/go-1.6/src/runtime/panic.go:481 +0x3e6
main.main()
	/home/franco/gocode/src/codigos/gowiki/wiki.go:36 +0x1ea

Any thoughts?

Thanks

It’s working fine for me on Windows.

Also, check the error returned from p2, _ := loadPage("TestPage") to see what could be going on.

Hi, Eric

Thanks for the reply.
I’m running it on Linux Mint.

I’ve try to comment the the following lines:

	//p2, _ := loadPage("TestPage")
	//fmt.Println(string(p2.Body))

and the error’s gone. but also, nothing else happens.
I think that are some problem with this loadPage function…

The stack trace shows the panic to originate at wiki.go line 36. The code you provided only has 33 lines.

I ran the code you provided on macOS 10.12 and ubuntu 16.04 (both 64-bit) and its output was:

This is a sample Page.

I do not see anything obviously wrong with save or loadPage. My guess is that you are encountering some sort of file permissions problem. The errors from ioutil.WriteFile and ioutil.ReadFile will tell you about this, if you don’t ignore them.

My preferred way of dealing with errors when working with prototype or exploratory code is to run

log.SetFlags(log.Lshortfile)

as the first line of main to tell the log package (import "log") to print the filename and line number of any log messages. When errors are encountered, I use

if err != nil {
	log.Fatalln(err)
}

as my default response to handling errors. This way I don’t have to think about error handling until I need to, and I get the benefits of not ignoring the errors.

The errors on lines 30 (p1.save) and 31 (p2, _ := loadPage("TestPage")) are not handled.

1 Like

OK, this is VERY embarrassing…

Mr. Nathan, You’re absolutely right about the file permissions.
I forgot to run my ./wiki with superuser permission. I was so focused in the code…

But at least I had the opportunity of learn about your error dealing method, very interesting, thank you.

Thanks to everybody for the patience!

Mr. Nathan, best regards from South America.

Glad I could help.

You should not need to run with superuser permissions. Since you are running ./wiki I assume you are running from your source directory, meaning your user should have the correct permissions. The tutorial will have the web server at port 8080, which can be opened by a regular user. A problem you could encounter is that files created by your wiki may be owned by root as you previously used superuser permissions. This can be fixed with chown or deleting the files (if they are not important).

I wrote more about this error handling method at https://pocketgophers.com/error-checking-while-prototyping/ :blush:

2 Likes

Thanks, problem solved.

I’m checking the link posted, very interesting indeed.

Having such positive first impressions with the go lang and community.

Best regards,

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.