Packages and Modules
Import Groups
Go import blocks list the packages a file uses.
import group
An import block keeps related imports together and makes package dependencies visible.
Import Groups
import_groups.go
Replay: real traced execution (multi-file project)
package main
import (
"fmt"
"sort"
"strings"
)
func main() {
var phrase = "go modules"
words := strings.Fields(phrase)
sort.Strings(words)
first := words[0]
fmt.Println("phrase=", phrase)
fmt.Println("count=", len(words))
fmt.Println("first=", first)
}
package main
import (
"fmt"
"sort"
"strings"
)
func main() {
var phrase = "package main"
words := strings.Fields(phrase)
sort.Strings(words)
first := words[0]
fmt.Println("phrase=", phrase)
fmt.Println("count=", len(words))
fmt.Println("first=", first)
}
package main
import (
"fmt"
"sort"
"strings"
)
func main() {
var phrase = "import path"
words := strings.Fields(phrase)
sort.Strings(words)
first := words[0]
fmt.Println("phrase=", phrase)
fmt.Println("count=", len(words))
fmt.Println("first=", first)
}
phrase ← "go modules", words ← []string{"go", "modules"}, first ← "go"
9func main() {10 var phrase→ "go modules" = "go modules" //@phrase="package main", "import path"11 words→ []string{"go", "modules"} := strings.Fields(phrase"go modules")12 sort.Strings(words[]string{"go", "modules"})13 first→ "go" := words[0]"go"1415 fmt.Println("phrase=", phrase"go modules")16 fmt.Println("count=", len(words[]string{"go", "modules"}))17 fmt.Println("first=", first"go")18}outputphrase= go modules count= 2 first= go
phrase ← "package main", words ← []string{"package", "main"}, first ← "main"
9func main() {10 var phrase→ "package main" = "package main"11 words→ []string{"package", "main"} := strings.Fields(phrase"package main")12 sort.Strings(words→ []string{"main", "package"})13 first→ "main" := words[0]"main"1415 fmt.Println("phrase=", phrase"package main")16 fmt.Println("count=", len(words[]string{"main", "package"}))17 fmt.Println("first=", first"main")18}outputphrase= package main count= 2 first= main
phrase ← "import path", words ← []string{"import", "path"}, first ← "import"
9func main() {10 var phrase→ "import path" = "import path"11 words→ []string{"import", "path"} := strings.Fields(phrase"import path")12 sort.Strings(words[]string{"import", "path"})13 first→ "import" := words[0]"import"1415 fmt.Println("phrase=", phrase"import path")16 fmt.Println("count=", len(words[]string{"import", "path"}))17 fmt.Println("first=", first"import")18}outputphrase= import path count= 2 first= import