2.6. Packages and files

Packages in Go are similar in concept to libraries or modules in other languages, with the goal of supporting modularity, encapsulation, separate codification, and code reuse. The source code of a package is stored in one or more source files with file name extensions of. Go. Usually, the suffix of the directory path of a package is the import path of the package. Such as package gopl. IO/ch1 corresponding directory path is $GOPATH/helloworld/SRC/gopl. IO/ch1 / helloworld. Each package corresponds to a separate namespace. For example, the Decode function in the Image package is different from the Decode function in the Unicode/UTF16 package. To reference this function externally, you must explicitly access it using image.Decode or UTf16.decode. Packages also allow us to hide internal implementation information by controlling which names are externally visible. In Go, a simple rule is that if a name begins with a capital letter, the name is exported. To demonstrate the basic usage of the package, let’s assume that our temperature conversion software is already popular and that we want to use the package in the Go language community. How do we do that? Let’s create a package called gopl.io/ CH2 /tempconv, which is an improved version of the previous example. The package code is stored in two source files to demonstrate how to declare it in one source file and then access it in the other; In reality, though, such a small package generally requires only one file. We put the variable declarations, corresponding constants, and methods into the tempConv.go source file:

// Package tempconv performs Celsius and Fahrenheit conversions.
package tempconv
import "fmt"
type Celsius float64
type Fahrenheit float64
const (
AbsoluteZeroC Celsius = 273.15
FreezingC Celsius = 0
BoilingC Celsius = 100
) 
func (c Celsius) String(a) string { return fmt.Sprintf("% g ° C", c) }
func (f Fahrenheit) String(a) string { return fmt.Sprintf("% g ° F", f) }
Copy the code

The conversion function is placed in another conv.go source file:

package tempconv
// CToF converts a Celsius temperature to Fahrenheit.
func CToF(c Celsius) Fahrenheit { return Fahrenheit(c*9/5 + 32)}// FToC converts a Fahrenheit temperature to Celsius.
func FToC(f Fahrenheit) Celsius { return Celsius((f - 32) * 5 / 9)}Copy the code

2.6.1. Import packages

In Go, each package has a globally unique import path. IO/CH2 / tempConv “in the import statement corresponds to the package import path. The specification of the Go language does not define the exact meaning of these strings or where packages come from; they are interpreted by the build tool. When using the Go toolkit that comes with the Go language (Chapter 10), an import path represents one or more Go source files in a directory. In addition to the package import path, each package also has a package name, which is usually a short name (it is not required that the package name be unique), specified in the package declaration. By convention, the name of a package is the same as the last field in the package import path, e.g. Gopl.io/CH2 / tempConv the package name is tempConv. To use the gopl. IO/CH2 /tempconv package, you need to import it first

/ Cf converts its numeric argument to Celsius and Fahrenheit.
package main
import (
"fmt"
"os"
"strconv"
"gopl.io/ch2/tempconv"
) 
func main(a) {
    for _, arg := range os.Args[1:] {
    t, err := strconv.ParseFloat(arg, 64)
    iferr ! =nil {
        fmt.Fprintf(os.Stderr, "cf: %v\n", err)
        os.Exit(1)
    } 
    f := tempconv.Fahrenheit(t)
    c := tempconv.Celsius(t)
    fmt.Printf("%s = %s, %s = %s\n",
    f, tempconv.FToC(f), c, tempconv.CToF(c))
    }
}
Copy the code