imported and not used "errors"

125 views
Skip to first unread message

Conor O'Neill

unread,
Sep 27, 2022, 11:15:11 AM9/27/22
to golan...@googlegroups.com
I am following a tutorial to great an API in Go with a framework called Gin.

I have imported errors and created a function like so;

func createBook(c *gin.Context) {

   var newBook book

 

   if err := c.BindJSON(&newBook); err != nil {

       return

   }

 

   books = append(books, newBook)

   c.IndentedJSON(http.StatusCreated, newBook)

}


func main is as follows;

func main() {

   //1.router responsible for handling different routes and diff endpoints of API

   router := gin.Default()

   //2.the route we are handling is /books, ie when u go to localhost:8080/books it will call getBooks()function

   router.GET("/books", getBooks)

   router.POST("/books", createBook)

   router.Run("localhost:8080")

}


If i simply ignore the import error I get this;

[GIN-debug] Listening and serving HTTP on localhost:8080

[GIN-debug] [ERROR] listen tcp 127.0.0.1:8080: bind: address already in use


I've gone over the tutorial several times and I'm not sure how he isn't getting the erros.

On another note are there any good recommendations for documentation to create an API without a framework?

Steven Hartland

unread,
Sep 27, 2022, 12:02:14 PM9/27/22
to Conor O'Neill, golan...@googlegroups.com
Something is already running on localhost port 8080. use a different port.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAPp9YQWGyicjMrFrMD2yg4Sb%2BLMGFFzGq_aKbRf25iLv2YW1Nw%40mail.gmail.com.

Kurtis Rader

unread,
Sep 27, 2022, 12:59:16 PM9/27/22
to Conor O'Neill, golan...@googlegroups.com
I find the Gin documentation (and the examples provided by the project) to be, ahem, lacking in important details. As you can see from the source (https://github.com/gin-gonic/gin/blob/78dad9d77d8c2d679dedea1fbef5fc8a54372efd/gin.go#L372) the Run() function returns an error that will tell you binding to port 8080 failed.

While Gin may be a great project (I've never used it) you don't need a framework like it for your simple example The standard net/http package works just fine for simple use cases.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAPp9YQWGyicjMrFrMD2yg4Sb%2BLMGFFzGq_aKbRf25iLv2YW1Nw%40mail.gmail.com.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank
Reply all
Reply to author
Forward
0 new messages