前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Golang: Packages - Introduction

Golang: Packages - Introduction

作者头像
Miigon
发布2022-10-27 15:54:14
2300
发布2022-10-27 15:54:14
举报
文章被收录于专栏:Miigon's BlogMiigon's Blog

Packages - Introduction

Every Go program is made up of packages.

Note: Do not get confused with Go modules, which is Go’s dependency management system. A Go modules usually contains one or more Go packages. We will discuss Go modules later, for now, just remember that we are talking about packages and just pretend modules don’t exist.

Programs start running in package main.

代码语言:javascript
复制
package main

import (
	"fmt"
	"math/rand"
)

func main() {
	fmt.Println("My favorite number is", rand.Intn(10))
}

Use package at the beginning of the code to specify the current package. Use import to import other packages.

Import

import can take the following two styles:

代码语言:javascript
复制
import (
	"fmt"
	"math/rand"
)
代码语言:javascript
复制
import "fmt"
import "math/rand"

The prior grouped style is the recommeded “good” style by Go.

Exported names and non-exported names

In Go packages, only exported names can be accessed outside the same package.

A name is exported if it begins with a capital letter.

Examples:

  • Pizza is an exported name since it starts with capital letter P
  • Pi starts with a capital letter thus is an exported name from package math. (Accessible with math.Pi)
  • Println is an exported name of package fmt
  • Foobar will be an exported name if you ever use it in your package.
  • foobar however, will NOT be exported if you use it in your package. It’ll be “unexported” thus inaccessable from outside the package.

By designing the language this way, Go effectively forced you to name your functions and types using a standard and preselected naming style. (“CamelCase” for exported names, “lower camelCase” for internal unexported names)

Whether that’s a good thing or a bad thing is up for debate, but this design decision ditched the need of the keyword public, export or extern commonly found in other languages, making programmers’ lives easier. It also makes the naming in all packages more unified and predictable, which is a good thing.

Package Scope

There’s no “global” scope in Go, instead, all functions and variables that is not local is in the package scope.

代码语言:javascript
复制
package myPackage

var v1, v2, v3 int			// in package scope 'myPackage'
var Var1 int				// in package scope 'myPackage', exported

func myfunction() int {}    // also in package scope 'myPackage'

func AnotherFunc() int {}	// in package scope 'myPackage', but it's also "exported" so 
							// it is accessible outside of package 'myPackage' by using
							// `myPackage.AnotherFunc()`

Functions and variables within package scope is accessible within the whole package. Whether a name is accessible from outside the package however, is determined by the naming style of the name. See Exported names and non-exported names.

In this case, AnotherFunc() can be accessed in another package like this:

代码语言:javascript
复制
package main

import (
	"fmt"
	"path/to/myPackage"				// we will talk about this later
)

func main() {
	myPackage.AnotherFunc()			// works
	fmt.Println(myPackage.Var1)		// works
	// myPackage.myFunction()		// this doesn't work since `myFunction` is not exported
}

This is just for showcasing the difference between exported names and non-exported names, as well as the idea that you can access functions and variables in other packages.

Conclusion

We’ll be covering more about packages in a future article. For now, knowing how to use packages is enough.

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Packages - Introduction
    • Import
      • Exported names and non-exported names
        • Package Scope
        • Conclusion
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档