为什么频道无法运作?

I am studying ‘Go Concurrency Pattern' from https://talks.golang.org/2012/concurrency.slide#25'

Question:

  1. How the channel share variable from it's outside ? In this case i has been shared. Variable at point A and point B seems to have some special relation ? What is it ?

  2. What does it means for ?

    for i := 0; ; i++

Main code:

package main

import (
    "fmt"
    "math/rand"
    "time"
)

func boring(msg string) <-chan string { // Returns receive-only channel of strings.

    c := make(chan string)
    go func() { // We launch the goroutine from inside the function.
        for i := 0; ; i++ {           // <--------- point B
            c <- fmt.Sprintf("%s %d", msg, i)

            time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond)
        }
    }()
    return c // Return the channel to the caller.
}

func main() {
    c := boring("boring!") // Function returning a channel.
    for i := 0; i < 5; i++ {               // <--------- point A
        fmt.Printf("You say: %q
", <-c)
    }
    fmt.Println("You're boring; I'm leaving.")
}

output:

You say: "boring! 0"
You say: "boring! 1"
You say: "boring! 2"
You say: "boring! 3"
You say: "boring! 4"
You're boring; I'm leaving.
Program exited.

The for (i := 0; ; i++) { } creates an index the increments forever.

When you make(chan string ) you've created a read/write channel. You also reference the channel inside the go function and pass it out as the return value. Go does an analysis of how variables are used called "escape analysis" and chooses whether to make the channel on the heap or on the stack, in your case on the heap so that when the function creating the channel exits the channel doesn't get deallocated.