zoukankan      html  css  js  c++  java
  • 11.exporting 导出

    Declare and access exported identifiers - Pkg

    // Package counters provides alert counter support.
    package counters
    
    // AlertCounter is an exported named type that
    // contains an integer counter for alerts.
    type AlertCounter int
    

    Declare and access exported identifiers - Main

    
    // Sample program to show how to access an exported identifier.
    package main
    
    import (
    	"fmt"
    
    	"github.com/ardanlabs/gotraining/topics/go/language/exporting/example1/counters"
    )
    
    func main() {
    
    	// Create a variable of the exported type and initialize the value to 10.
    	counter := counters.AlertCounter(10)
    
    	fmt.Printf("Counter: %d
    ", counter)
    }
    

    Declare unexported identifiers and restrictions - Pkg (

    // Package counters provides alert counter support.
    package counters
    
    // alertCounter is an unexported named type that
    // contains an integer counter for alerts.
    type alertCounter int
    

    Declare unexported identifiers and restrictions - Main

    // Sample program to show how the program can't access an
    // unexported identifier from another package.
    package main
    
    import (
    	"fmt"
    
    	"github.com/ardanlabs/gotraining/topics/go/language/exporting/example2/counters"
    )
    
    func main() {
    
    	// Create a variable of the unexported type and initialize the value to 10.
    	counter := counters.alertCounter(10)
    
    	// ./example2.go:17: cannot refer to unexported name counters.alertCounter
    	// ./example2.go:17: undefined: counters.alertCounter
    
    	fmt.Printf("Counter: %d
    ", counter)
    }
    

    Access values of unexported identifiers - Pkg

    // Package counters provides alert counter support.
    package counters
    
    // alertCounter is an unexported named type that
    // contains an integer counter for alerts.
    type alertCounter int
    
    // New creates and returns values of the unexported type alertCounter.
    func New(value int) alertCounter {
    	return alertCounter(value)
    }
    

    Access values of unexported identifiers - Main

    // Sample program to show how the program can access a value
    // of an unexported identifier from another package.
    package main
    
    import (
    	"fmt"
    
    	"github.com/ardanlabs/gotraining/topics/go/language/exporting/example3/counters"
    )
    
    func main() {
    
    	// Create a variable of the unexported type using the exported
    	// New function from the package counters.
    	counter := counters.New(10)
    
    	fmt.Printf("Counter: %d
    ", counter)
    }
    

    Unexported struct type fields - Pkg

    // Package users provides support for user management.
    package users
    
    // User represents information about a user.
    type User struct {
    	Name string
    	ID   int
    
    	password string
    }
    

    Unexported struct type fields - Main

    // Sample program to show how unexported fields from an exported struct
    // type can't be accessed directly.
    package main
    
    import (
    	"fmt"
    
    	"github.com/ardanlabs/gotraining/topics/go/language/exporting/example4/users"
    )
    
    func main() {
    
    	// Create a value of type User from the users package.
    	u := users.User{
    		Name: "Chole",
    		ID:   10,
    
    		password: "xxxx",
    	}
    
    	// ./example4.go:21: unknown users.User field 'password' in struct literal
    
    	fmt.Printf("User: %#v
    ", u)
    }
    

    Unexported embedded types - Pkg

    // Package users provides support for user management.
    package users
    
    // user represents information about a user.
    type user struct {
    	Name string
    	ID   int
    }
    
    // Manager represents information about a manager.
    type Manager struct {
    	Title string
    
    	user
    }
    

    Unexported embedded types - Main

    // Sample program to show how to create values from exported types with
    // embedded unexported types.
    package main
    
    import (
    	"fmt"
    
    	"github.com/ardanlabs/gotraining/topics/go/language/exporting/example5/users"
    )
    
    func main() {
    
    	// Create a value of type Manager from the users package.
    	u := users.Manager{
    		Title: "Dev Manager",
    	}
    
    	// Set the exported fields from the unexported user inner type.
    	u.Name = "Chole"
    	u.ID = 10
    
    	fmt.Printf("User: %#v
    ", u)
    }
    
  • 相关阅读:
    POJ 1426 Find The Multiple(数论——中国同余定理)
    POJ 2253 Frogger(Dijkstra变形——最短路径最大权值)
    POJ 3790 最短路径问题(Dijkstra变形——最短路径双重最小权值)
    POJ 3278 Catch That Cow(模板——BFS)
    HDU 1071 The area
    HDU 1213 How Many Tables(模板——并查集)
    POJ 1611 The Suspects
    light oj 1214 Large Division
    POJ 1258 Agri-Net(Prim算法求解MST)
    POJ 2387 Til the Cows Come Home(模板——Dijkstra算法)
  • 原文地址:https://www.cnblogs.com/zrdpy/p/8592033.html
Copyright © 2011-2022 走看看