Packages
Every Go program consists ofpackages
. Each package needs to be imported first to use its exported identifiers.
You can importpackages
like the code below.
packagemainimport("fmt"// Imported Package"math/rand"// Imported Package)funcmain(){fmt.Println("My journey of Go",rand.Intn(10))//=>My journey of Go 1}
You can also import each package individually.
import"fmt"import"math/rand"
Exported names
In Go, a name is exported if it begins with a capital letter. For instance,Pi
is a name that is exported from'math'
package.
packagemainimport"math"funcmain(){fmt.Println(math.Pi)//=> 3.141592653589793}
Functions
A function can take zero or more arguments. You can define a function like the code below.
funcname_of_the_function(arguments)type{[contentofthefunction]}
Don't forget to write the types of arguments and the return value of a function.
packagemainimport"fmt"funcmain(){fmt.Println(greetings("John"))//=> Hello John}funcgreetings(namestring)string{return"Hello"+" "+name}
Omission of the type of arguments
When there are two or more parameters sharing the same type, you only have to write the type once.
packagemainimport"fmt"funcmain(){fmt.Println(add(2,4))//=> 6}funcadd(x,yint)int{returnx+y}
Multiple results
A function can return any number of results.
packagemainimport"fmt"funcmain(){a,b:=multipleArgs("Hello","World")fmt.Println(a,b)//=> World Hello}funcmultipleArgs(arg1,arg2string)(string,string){returnarg2,arg1}
Variables
Thevar
statement declares a list of variables. As in function argument lists, the type of a variable is written after the name of the variable.
Variables that are declared without an initial value are given their zero value. For instance,0
is for numeric types,false
is for the boolean type and""
(the empty string) is for strings.
packagemainimport"fmt"varvar1,var2,var3boolfuncmain(){varnumintfmt.Println(num,var1,var2,var3)//=> 0 false false false}
As you can see in the code above, Avar
statement can be used at package or function level.
Variables with initializers
Avar
declaration can include an initializer.
If there is an initializer, the type of the variable can be omitted. The variable will take the type of the initializer.
packagemainimport"fmt"funcmain(){varstr1="ruby"varstr2,num1,num2="go",2,4fmt.Println(str1,num1,str2,num2)//=>ruby 2 go 4}
Short variable declarations
You can use:=
to declare a variable inside a function. Go raises an error if you try to declare a variable with:=
outside a function.
packagemainimport"fmt"funcmain(){str:="Js"num:=4boole:=truefmt.Println(str,num,boole)//=> Js 4 true}
Basic types
Go's basic types are listed in the code below.
boolstringintint8int16int32int64uintuint8uint16uint32uint64uintptrbyte// alias for uint8rune// alias for int32// represents a Unicode code pointfloat32float64complex64complex128
Type conversions
You can not implicitly change the type of a variable in Go. Go raises an error if you try to reassign a value that type is different form the original value type like the code below.
variint=100varffloat64=i//=> // cannot use i (type int) as type float64
If you want to change the type of a variable, you can do so like the code below.
varauint32=1234567890varbuint8=uint8(a)fmt.Println(b)// 210
Constants
Constants are declared with theconst
keyword.
Go supports constants of character, string, boolean, and numeric values.
Constants cannot be declared using the:=
syntax.
packagemainimport"fmt"constName="Go"funcmain(){constNum=1constTruth=truefmt.Println(Name,Num,Truth)//=> Go 1 true}
Top comments(2)

- Email
- LocationAustin, TX
- EducationMaster's Degree, Information Communication Technology
- Joined
Great intro so far!

Thank you!
For further actions, you may consider blocking this person and/orreporting abuse