Question: ```
package main

import(
    "fmt"
    "time"
)

func main(){
    fmt.Println("1")
    defer fmt.Println("-1")
    go func() { 
        fmt.Println("2")
        defer fmt.Println("-2")
        time.Sleep(9 * time.Second)
    }()
    time.Sleep(1 * time.Second)
    fmt.Println("3")
}

```

Produces the output: 1 2 3 -1
But I would've thought the goroutine's defer would have been called to produce: 1 2 3 -2 -1

In my actual code my goroutine is blocked on a websocket... I suppose I could send a shutdown signal but I haven't yet figured out how to do a wait-on-multiple-objects kind of thing (if it can actually be done in go). I'm currently solving my problem by hoisting the deferred -2 into the main scope.

Is there some trick to defer placement I'm not doing right?

<http://play.golang.org/p/qv8UEuF2Rb>

Answer: 
The process is ending, so the internal function is terminated. Try sleeping at the end of main:

```
func main(){
    fmt.Println("1")
    defer fmt.Println("-1")
    go func() { 
        fmt.Println("2")
        defer fmt.Println("-2")
        time.Sleep(9 * time.Second)
    }()
    time.Sleep(1 * time.Second)
    fmt.Println("3")
    time.Sleep(9 * time.Second)
}

```

<http://play.golang.org/p/Qrtz_0xiRs>

EDIT: as mentioned in the comments, this is not the right approach in general. You should explicitly wait for the goroutine.
[WaitGroup](https://golang.org/pkg/sync/#WaitGroup) can help you resolving the issue: 

```
package main

import(
    "fmt"
    "sync"
    "time"
)

func main(){
    fmt.Println("-start main-")
    defer fmt.Println("-defer main-")

    wg := sync.WaitGroup{}
    wg.Add(1)

    go func() { 
        fmt.Println("-start goroutine-")
        defer fmt.Println("-defer goroutine-")

        time.Sleep(2 * time.Second)
        wg.Done() 
    }()

    fmt.Println("-end main-")
    wg.Wait()
}

```

Prints:

```
-start main-
-end main-
-start goroutine-
-defer goroutine-
-defer main-

```

<https://play.golang.org/p/ycmPJJ6pvC>