0% found this document useful (0 votes)
41 views37 pages

Hello World Golang

The document provides an introduction to Go programming including packages, importing packages, functions, parameters vs arguments, expressions vs statements, variables, constants, literals, and the go run, go build, and go install commands. Code examples and explanations are given for core Go concepts.

Uploaded by

thiagomatos.tlm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views37 pages

Hello World Golang

The document provides an introduction to Go programming including packages, importing packages, functions, parameters vs arguments, expressions vs statements, variables, constants, literals, and the go run, go build, and go install commands. Code examples and explanations are given for core Go concepts.

Uploaded by

thiagomatos.tlm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Hello World

code used in training


you can find all of the code shown in this training here:
https://github.com/GoesToEleven/GolangTraining
Every go file begins with a package name. The name
of the package must be the same as the folder name
except for package main. Package main is the entry
point for your program.

If you are using code from other packages, you list the
packages that you want to import. This allows you to The “fmt” package is being imported.
use code in your program that other people have
written. Packages are also sometimes referred to as
libraries.

A parameter is the variable which is part of


the func’s signature (func declaration). An
argument is an expression used when
calling the func. source: modified from stackoverflow
This function is declared with no (choose one):
● parameters
● arguments

“Hello world” is an example of a literal (choose one):


● parameter
● argument
Every go file begins with a package name. The name
of the package must be the same as the folder name
except for the main package. The main package is
the entry point for your program.

If you are using code from other packages, you list the
packages that you want to import. This allows you to The “fmt” package is being imported.
use code in your program that other people have
written. Packages are also sometimes referred to as
libraries.

Code from the “fmt” package is being used. Println is a A parameter is the variable which is part of
function declared in the “fmt” package. For a function to be the func’s signature (func declaration). An
accessible to other packages, it must be Capitalized. This is argument is an expression used when
analogous to “public” in other languages. calling the func. source: modified from stackoverflow

The func main() is the entry point for your program; the first
code that will run. The package main can also have other
functions besides func main().

An expression specifies the computation of a


value by applying operators and functions to
operands. source: effective go Statements control execution. source: effective go
Every go file begins with a package name. The name
of the package must be the same as the folder name
except for the main package. The main package is
the entry point for your program.

If you are using code from other packages, you list the
packages that you want to import. This allows you to The “fmt” package is being imported.
use code in your program that other people have
written. Packages are also sometimes referred to as
libraries.

Code from the “fmt” package is being used. Println is a A parameter is the variable which is part of
function declared in the “fmt” package. For a function to be the func’s signature (func declaration). An
accessible to other packages, it must be Capitalized. This is argument is an expression used when
analogous to “public” in other languages. calling the func. source: modified from stackoverflow

The func main() is the entry point for your program; the first
code that will run. The package main can also have other
functions besides func main().

An expression specifies the computation of a


value by applying operators and functions to
operands. source: effective go Statements control execution. source: effective go
http://www.quora.com/Whats-the-difference-between-a-statement-and-an-expression-in-Python
“Hello world” is a (choose one):
● statement
● expression
If I hold down “cmd” and click Println ...
I am taken to the source code!
If I hold down “cmd” and click fmt ...
I am taken to the source code!
go run gofiles…
source: command go
go build
As this is run in the folder with package main, it builds the file into an executable binary; if
this was run in a folder that was just a library package, it would build it and show you if
there were errors but not create a binary executable (it throws away the results).
go install
As this is run in the folder with package main, it builds the file into an executable binary
and puts it in “bin” under your workspace; if this was run in a folder that was just a library
package, it would build it and put the results in “pkg” under your workspace.
go install
As this is run in the folder with package main, it builds the file into an executable
binary and puts it in “bin” under your workspace; if this was run in a folder that was
just a library package, it would build it and put the results in “pkg” under your workspace.
go install
As this is run in the folder with package main, it builds the file into an executable binary
and puts it in “bin” under your workspace; if this was run in a folder that was just a
library package, it would build it and put the results in “pkg” under your
workspace.
Once a program has been compiled and put into “bin” …
… as I have a path variable pointing to the bin folder …
… I can run my program by typing the program’s name …
… in this case, “hello”
Once a program has been compiled and put into “bin” …
… as I have a path variable pointing to the bin folder …
… I can run my program by typing the program’s name …
… in this case, “hello”
If I comment it out ...

… it doesn’t run.
my go workspace
Review
● code completion ● go run
● package main ○ go run gofiles...
○ func main() ● go build
■ entry point for your program ● go install
● packages, aka, libraries
● import
● functions
○ What makes a function accessible outside a
package
■ capitalization
● makes the function “public”
■ lowercase
● function restricted to package
● parameters vs arguments
● expressions vs statements
● variable, constant, literal
● cmd + click → takes you to source code
Review Questions
Package Main
● What is the purpose of package main in a go program?
● What function must package main contain?
● Can package main contain a function called func
blueSky() ?
funcs
What makes a func accessible outside a
package?
Parameters vs Arguments
● What is the difference between the two?
Expressions vs Statements
● What is the difference between the two?
Variable, Constant, Literal
● Define the three concepts above.
● Give an example of a literal from the “hello
world” example.
go run
● Build “hello go” in your editor
● use go run from the command line to make
your “hello go” program execute
go build
Go build does what when run on a folder
containing package main?
go build
Go build does what when run on a folder
containing a library package?
go install
Go install does what when run on a folder
containing package main?
go install
Go install does what when run on a folder
containing a library package?

You might also like