Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

K-Sato
K-Sato

Posted on

     

My Journey of Go (Functions, Variables)

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}
Enter fullscreen modeExit fullscreen mode

You can also import each package individually.

import"fmt"import"math/rand"
Enter fullscreen modeExit fullscreen mode

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}
Enter fullscreen modeExit fullscreen mode

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]}
Enter fullscreen modeExit fullscreen mode

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}
Enter fullscreen modeExit fullscreen mode

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}
Enter fullscreen modeExit fullscreen mode

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}
Enter fullscreen modeExit fullscreen mode

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}
Enter fullscreen modeExit fullscreen mode

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}
Enter fullscreen modeExit fullscreen mode

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}
Enter fullscreen modeExit fullscreen mode

Basic types

Go's basic types are listed in the code below.

boolstringintint8int16int32int64uintuint8uint16uint32uint64uintptrbyte// alias for uint8rune// alias for int32// represents a Unicode code pointfloat32float64complex64complex128
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

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}
Enter fullscreen modeExit fullscreen mode

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
khrome83 profile image
Zane Milakovic
  • Email
  • Location
    Austin, TX
  • Education
    Master's Degree, Information Communication Technology
  • Joined

Great intro so far!

CollapseExpand
 
k_penguin_sato profile image
K-Sato
Software Engineer

Thank you!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

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

Software Engineer
  • Work
    Software Engineer
  • Joined

More fromK-Sato

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp