Go package

Last section we figured out how to run our project, now we're going to figure out what the very first line code inside main.go file means.

We'll first talk about the word package, and then we'll talk about why we use the word main.

What does the "pakcage" mean?

IF you see some packages in Go, you can think of them as being like Project or Workspace.

When you're working on a discrete application, you would traditionally be creating one single package.

A package is a conllection of common source code files, It has many related files inside it ,and each file ends with a file extension of .go

The only requirement for a package is that at the very first line of file must declare package that it belongs to.

For example, there are three files that all belong to a package,Then each file must have the statement "packge main" at the top of the file.

How many package type in Go?

There are two package types in Go, One is an executable type of package , and other is a reusable one.

Executable type of package

Do you remember that we ran go build main.go at the command line, and then it spat out a file called main.exe in the directory that the main.go file belongs to.

Why did it do so? Which line of the code inside the main.go file tells Go to do that like this?

If you look at the source code inside the main.go, you'll see a statement that says "package main" at the very first line of the file,

Actually,if you see the statement "package main" in the source code ,that means you're making an executable type of package.

Throughout the rest of sections, we're going to be making projects that primarily use the name "package main" because we always want to be creating something that we can instantly run or test.

The last thing I want to tell you is that If you want to create an executable type of package, you must declare a function called main in the source code of the file. Just like the follwing picture.

Reusable type of package

The resuable types of package can be thought of as code dependencies or libraries.

If you want to create a reusable package and put some reuable logic, helper functions or other stuff in it so that it can be reused in others' own personal projects. you can set a any other name besides "main" for the package.

For example "package blahblah"

The original video and subtitles

video:Go Packages

subtitles:

In the last section, we figured out how to run code inside our projects.

We're going to move on to figuring out exactly what that first line of code inside of our main.go means. Specifically, the line that says package main.

We'll first talk about the word package. And we'll talk about why we use the word main right here.

Let me put up a diagram.

So when you see a package in Go, you can think of the package as being like a project of a workspace.

A package is a collection of common source code files.

So if you and I are working on one discrete application like we're working on one app right now we would traditionally be creating one single package.

So a package can have many related files inside it, each file ending with a file extension of go.

The only requirement for the files inside of a package is at the very first line of each file must declare the package that it belongs to.

So for example, there are three files that all belong to Package Main and then each file must have a statement Package Main at the very top line, just like our current main.go file does right here.

So if we had two other files inside of this package, they would also need to declare Package Main at the top.

Now until you know a little bit about exactly why we called our package main.

You know why we call it main.

Why didn't we call it Hello World to name the folder that it belongs to.

Well inside of Go there are two different types of package.

There is an executable type and a reusable type.

An executable type of package is one that when we compiled spits out an actual runnable file or executable file just like what we saw when we executed go bulid command at our command line.

Remember when we ran go build main.go like that right here. It spit out this main file right here which we were then able to run and execute.

So this file right here was created specifically because we created an executable type package.

Executable packages are usually used for actually doing something.

And that is primarily what we are going to do in this course.

You know we are going to be writing programs that we can run and we can use them to accomplish tasks.

We also have access to reusable packages and you can think of these as being like code dependencies or libraries.

These are packages that are not used to say like double click on an executable. Instead we put in a lot of reusable logic or helper function or stuff that will just help us reuse some code on futureprojects in the future.

So you might be a bit curious . How do we know if we are making an executable package or a reusable one.

So how do we know when we are making one or the other?

If you look at our source code file clearly, there is noting really inside of here that say oh yeah spit out some executable file when you compile me.

So how do we know when we're making one or the other?

Well, it is actually a little bit tricky.

Remember that we called our package with the name of main.

So the very first line for us inside of our files said Package Main.

It's actually the name of the package that we use that determines whether you are making an executable or dependency type package.

So specifically, the word main is used to make an executable type package.

So we took a package main, we ran Go build on it and it spat out a file called main or main.exe if you are on windows system.

If we hadn't used any other name for our package besides Main , so if we call it package baba and then run go build. It would not spit out an executable file.

So the word package main is sacred.

It's one that we use only when we are making a package that we want to spit out some runnable file。

Throughout the rest of this course, you and I are going to be making projects that primarily use the name package man because we generally always want to be creating something that we can immediately run and test.

However if we were trying to make some library of reusable code or if we wanted to do some project that we could share with our friends, so they use our code on their own project, that's when we would start using a more specialized package name.

Now to summarize, basically, whenever we see the word package main, that means we are making an executable package, any other name whatsoever means we are making a reusable or dependency type package.

Now the last thing I want to tell you, Anytime we make an executable package,it must always have a function inside of it called Main as well.

So if we flip back over to our code editor, that's what the statement right here was we had said function main.

So we specifically created a function called main because we had called package main right here creating an executable type package and to just give you a quick demo of thes, I am going to go to the very first line of code up here and I am going to change the name of my package over to Apple.

Now I'm going to back over to my terminal, if I list out all of my files and folders. You will see that I still have the main executable right here.So I'm going to remove it, really quick.Now I'm back to only main main.go, if I build this file now with a different package name and then list out all my files and folders.You'll see that I dit not get an actual executable thing inside of here.

So obviously , the name of the package does matter.

I'm now going to change it back to a package name of main like so I'll go back over build my project.

And now I see that I got the executable here again.

So I think that's probably enough for packages right now.

Again, we're going to get a lot more experience with packages in the future.

So for now, let's just focus on our next question at hand.

Where we want to figure out exactly what the next line of code means.

So let us quickly break and start talking about that import statement in the next.

privious: How do we run the Go program?

next

相关推荐
啾啾Fun9 分钟前
【Java微服务组件】分布式协调P1-数据共享中心简单设计与实现
java·分布式·微服务
Brilliant Nemo11 分钟前
Vue2项目中使用videojs播放mp4视频
开发语言·前端·javascript
TNTLWT29 分钟前
Qt控件:交互控件
开发语言·qt
量化金策32 分钟前
震荡指标工具
开发语言
北漂老男孩34 分钟前
ChromeDriver进程泄漏问题分析与最佳实践解决方案
开发语言·爬虫
神经毒素38 分钟前
WEB安全--Java安全--shiro550反序列化漏洞
java·安全·web安全·shiro
李迟39 分钟前
Golang实践录:在go中使用curl实现https请求
开发语言·golang·https
hnlucky1 小时前
Windows 上安装下载并配置 Apache Maven
java·hadoop·windows·学习·maven·apache
运维-大白同学2 小时前
go-数据库基本操作
开发语言·数据库·golang
动感光博2 小时前
Unity(URP渲染管线)的后处理、动画制作、虚拟相机(Virtual Camera)
开发语言·人工智能·计算机视觉·unity·c#·游戏引擎