Five questions about the boring program.
Last section we put together a very simple,very basic and very boring program.
In this chapter and the next couple of chapters, we'll break this thing down line by line.
Maybe, you are going to be really surprised because even though this is a very boring program, we're going to go over it line by line.
I took the liberty of already kind of analyzing every line.
After I did so, I came up with five questions and We are going to use these to get a much better handle on the fundamental concepts around Go.
In this section, we will answer the first qustion.
How do we run the code in our project?
I put forward a diagram like the next picture.
Thoes are a couple of command-line tools that are attached to the CLI. And then we are going to talk about Go run and Go build,the two commands carefully. Others command ,we are going to go through very quickly.
Go run
Let's flip over to our terminal in the Cursor Editer before we run the program that we wrote out.
We can used Go run to run our projects like this:
Please make sure that the current directory of terminal has been navgated to the directory where you created your Hello World program.
The following video will demostrate how to run a Go project.
How to run our project?
Go build
The Go build command is very similar to Go run command, so we'll take a bit of time to make sure that the difference between the two commands is at least reasonably clear.
Let's build our main.go file through the next command-line.
When I do, you will see that we don't get any message printed on the screen.
But if we list out all files and folders in this directory, you'll locate a new file called main.exe that was built out of source code inside main.go.
If you are on a Mac or Linux system, this file is reflected as main.
Now it is kind of clear. The Go run command is used to compile and instantly execuate file.The Go build command is used to just compile it.
Go fmt
The Go fmt command is used to automatically format all code of different files inside our projects.
There is a pretty good example like this.
Go get, Go install
Go get comand and Go install command are used to handle the dependencies in out project.
If we want to get some code that has written by someone else we can use this commands to get access to our own personal projects.
Go test
Go test command is used to run and execute test files that are associated with our projects.
How do we run the code in our projects?
So let us start answering the question number one, How do we run our project?
We use the Go command tools, we can intantly run the files or we can build them and then run it in the future, At some point ourself.
Ok, let us take a quick break and continue with our next question.
The original video and subtitles
video: Five Important Questions
subtitles:
In the last section, we put together a very tiny, very basic, very boring program.
And so I am not going to lie, Yes a hello world type program is very boring.
And I know it is something that you do not like to see and, of course, you want to see something more usable, more flashy, more interesting.
However, I think you are going to be really surprised here because even though this is a very boring program, we are going to go over line by line.
And I think you are going to be very surprised to see how much knowledge you can gain by studying every line of code inside this file.
So in this video and the next couple of vides we are going to break this thing down line by line and use it to get a much better handle on some of basic fandamental concepts around Go.
Now I took the liberty of already kind of analyzing every line.
And after I did so I came up with five questions that we can use to get a better sense on what that program is doing .
So these are file questions I want to answer and soon we answer these we will have a better sense of the fundamentals of Go.
So first off, how do we run the code inside of our projects like we're written some code out.
But how do we actually execute it. How do we run it.
I also want to know what the very first line of program means, specifically the line that says package main.
I want to figure out what the package, what the main and what good it is for us.
Next, I want to figure out the next line down : "import fmt"
I want to figure out what that function at the bottom is for?
And then finally, it looks like the code inside the main.go file is organized in some fashion.
Right because we specifically put package main at the top,and then we did import fmt and then we wrote a func
And so, I want to get a better sense of whether or not there is a general pattern that we want to follow whenever we are writing Go code.
So these are five questions that we're going to try to answer by studying the code inside of this very simple,very straightforward program
Let us start right now with question number one.
How do we run the code inside of our project .
I am going to flip on over to my terminal.
Here is my terminal and you will notice that I have already navigated to my project directory of hello world.
Inside here, I have got my main.go file.
So at this point, you should pause the video and pop open your terminal and navigate to where you created the project directory.
Now that I am inside of here and I am going to make use of that go command that we tested out earlier.
Remember we entered just go by itself and we were able to get help message right here.
Remeber the Go command is kind of our portal to working with all things Go on our local machine.
So this Go command give us ability to compile and execuate projects that we put together.
Let's figure out how to run our first project file.
We are going to write run "go run main.go".
When we do so, we will instantly see a message "Hi there" printed out on the screen.
So Hey, that is an easy one to run the code in our project.
We write "go run" and then the name of the file that we want to execute.
Now obviously I want to tell you a lot more about how we run code inside of our project .
So let us break down this command and figure out what it can really do for us.
All right so we are going to pull up a diagram here.
And this a diagram of some of the different command that are available to us with GLI.
So we just tested out command Go run.
Go Run can take one, two , three, four and a handful of files it compiles all code in those files and then instantly executes the reust.
So any time we have like one or two files, we say Go run and the name of the files one or more we want to compile and execute and boom off the races we go
On the other hand, we have a Go command called Go build.
Go bulid is a very like Go run. That is why I spend little bit of time to make sure that diference between the two command is at least reasonably clear
So Go run is used to compile and immediately exectue a program.
On the other hand Go build is just used to compile program. So Go build just compile it and it does not actually execute it.
If we flip back over to command line and run go build main.go and you will see that we do not get any message printed on the screen.
If you are on windows, you will probably see this reflected as main.exe
So this is an actual executable file that was build out of our source code of main.go.
If you are on windows or see if you are on Mac, you can now run this file by running slash main.
If you are on windows, you can run this main.exe to execute this file.
So again difference between Go run and Go build, Go build compiles the file and Go run compiles and executes it.
Now some of the other comannd tools that are attached to the Go CLI will go through very quickly.
We're going to cover a couple of thest later on in the course.
The Go format is used to automatically format all the code inside of all of our different files.
We will see a pretty good example of go format very shortly when we start working on our first project.
Go install and Go get our two commnads taht are used to handle dependencies inside of our projects.
So if we want to use code that has written by someone else we can use these commands to get access to it on our own personal projects.
Finally, the Go Test is used to run and execute and test files that are associated with the current project.
We are definitely going to see a good example of this commands as well.
All right, so I think that kind of answered question number one here.
How do we run the code inside of our project.
We use the go command line tool.
We can instantly run the file or we can build it and then run it in the future at some point ourself.
So I think that is the answer to question number one.
Let us take a quick break and then continue with our next question and the next video.
So I will see you in just a minute.
previous: Hello world program of Go
next: Go package