0

I am learning from a book the book says to add a couple lines of code, but it throws an error

package main

import (
   "./lissajous"
   "log"
   "net/http" 
)

func main() {

   handler := func(w http.ResponseWriter, r *http.Request) {
        lissajous(w)
   }
   http.HandleFunc("/", handler) // each request calls handler
   log.Fatal(http.ListenAndServe("localhost:8000", nil))
}

it throws this error

./server.go:6: imported and not used:
"_/home/zach/Dropbox/Personal/Go_Programming
         /The_Go_Programming_Language/ch01/web/lissajous"
./server.go:14: use of package lissajous without selector

the package directory I think is correct, but can't seem to call functions, the set up for the handler is just like that in the book am I missing something?

any help appreciated?

  • is your book telling you to do that import **"./lissajous"** ? well if that's the case, it isn't recommended. https://golang.org/doc/code.html – Yandry Pozo Oct 09 '17 at 04:26
  • 1
    Possible duplicate of [How to use custom packages in golang?](https://stackoverflow.com/questions/15049903/how-to-use-custom-packages-in-golang) – Yandry Pozo Oct 09 '17 at 04:27
  • the book doesn'y say to import the module, but to add the line : handler := func(w http.ResponseWriter, r *http.Request) { lissajous(w) } – Zachary Schiff Oct 10 '17 at 11:30

1 Answers1

0

Here are 2 things:

  • Go doesn't allow unused imports - if you import module you should use it, or temporarily comment out

  • another way of custom module creation and import recommended. Think like once you will publish it and treat it as being already published. Choose an unique URL for it like github.com/login/repo/super-package/package. However if you are not obliged to publish it right now neither in open source nor private repo. It's recommended to keep it like this in your system. It'lll be easier to start sharing it with colleagues and possibly publish if you wish.

Eugene Lisitsky
  • 12,113
  • 5
  • 38
  • 59
  • I was attempting to add the line `handler := func(w http.ResponseWriter, r *http.Request) { lissajous(w) }` which i assume would require in import if the package – Zachary Schiff Oct 10 '17 at 11:32