Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Latest commit

 

History

History
48 lines (33 loc) · 1.21 KB

imports.md

File metadata and controls

48 lines (33 loc) · 1.21 KB

Imports

There are two types of imports available in Go. In both cases, we generate the same reference to the package itself. This is done by creating an importMoniker. This import moniker

import "fmt"
//      ^^^------ reference github.com/golang/go/std/fmt

import f "fmt"
//     ^--------- local definition
//        ^^^---- reference github.com/golang/go/std/fmt


// Special Case, "." generates no local def
import . "fmt"
//                no local def
//        ^^^---- reference github.com/golang/go/std/fmt

Example

So given this kind of import, you will see the following.

import (
	"fmt"
	. "net/http"
	s "sort"
)
  • Regular "fmt" import. Creates only a reference to the moniker

fmt_import

  • Named s "sort" import. Creates both a reference and a definition. Any local references to s in this case will link back to the definition of this import. "sort" will still link to the external package.

sort_import

s_definition

  • . import. This will also only create a reference, because . does not create a new definition. It just pulls it into scope.

http_import