2

I'm doing some light web development in go, and running into a problem with entr. I'm editing .go files in a directory while having

ls *.go | entr -r go run *.go

running in a separate terminal window.

I can see it re-starting my program every time I save a file, because some format statements get printed out to the terminal every time I do so. However, whenever I navigate to localhost:8080, I see the handler content that was present when I started entr (rather than the content in the most recent change). If I Ctrl+C, then restart entr, I see the latest changes.

Any idea what I'm doing wrong?

Here's the minimal example:

// test.go
package main

import (
    "fmt"
    "net/http"
)

func handler (w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello there! I love %s!", r.URL.Path[1:])
}

func main() {
    fmt.Println("Starting up...")
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}

Now, if I run ls test.go | entr -r go run test.go, I see Starting up... printed to the terminal, and I can go to localhost:8080/tests to see a page that says "Hello there! I love tests!". If I change the handler function so that it reads

func handler (w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "I guess %s are ok...", r.URL.Path[1:])
}

I see the Starting up... message printed in terminal again, but going to localhost:8080/tests still gives me the page showing "Hello there! I love tests!" (even in incognito mode). If I then kill the entr process and restart it, I can go to localhost:8080/tests to see I guess tests are ok... as expected.

Edit:

Getting the error out of ListenAndServe confirms that this has to do with a dangling socket.

...
   err := http.ListenAndServe(":8080", nil)
   fmt.Printf("ListenAndServe ERR: %s\n", err)
...

shows the extra line

listen tcp :8080: bind: address already in use

in terminal. A quick search tells me that there isn't an easy way to stop a listener started with ListenAndServe. Is there a simpler approach than this?

2
  • 2
    If you would handle the errors ListenAndServe returns you would see what is wrong (and we could help). Probably your port is still in use and there is not much you can do about it except properly shutting down your app.
    – Volker
    Mar 24, 2015 at 16:22
  • 1
    As a side note, something like go list -f '{{join .GoFiles " "}}' is probably safer than using *.go to either ls or go run (e.g. that will skip *_test.go files and honor build constraints). You should probably also be using go build instead of go run (or go build && ./yourProgName).
    – Dave C
    Mar 24, 2015 at 20:14

3 Answers 3

2

The ls command only lists the go files in your current directory. The command should be like this to listen to go files in all subfolders in the project.

find . | grep '\.go' | entr -r go run .
1

The problem: go run will start the server in a separate process which won't be killed.

A solution: Use a little script which kills and starts the server

#!/bin/sh
pkill "$1"
go run "$1.go"

and use this for entr.

1

In linux there are two process created. First is the go run main.go and the second is the /tmp/go-build/b001/exe/main. The second one needs to be killed to terminate the program not the first...

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.