DEV Community

K-Sato
K-Sato

Posted on

My Journey of Go (Functions, Variables)

#go

Packages

Every Go program consists of packages. Each package needs to be imported first to use its exported identifiers.

You can import packages like the code below.

package main

import (
  "fmt" // Imported Package
  "math/rand" // Imported Package
)

func main(){
  fmt.Println("My journey of Go", rand.Intn(10)) //=>My journey of Go 1
}
Enter fullscreen mode Exit fullscreen mode

You can also import each package individually.

import "fmt"
import "math/rand"
Enter fullscreen mode Exit 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.

package main

import "math"

func main(){
  fmt.Println(math.Pi) //=> 3.141592653589793
}
Enter fullscreen mode Exit fullscreen mode

Functions

A function can take zero or more arguments. You can define a function like the code below.

func name_of_the_function(arguments) type {
  [content of the function]
}
Enter fullscreen mode Exit fullscreen mode

Don't forget to write the types of arguments and the return value of a function.

package main

import "fmt"

func main(){
  fmt.Println(greetings("John")) //=> Hello John
}

func greetings(name string) string {
  return "Hello" + " " + name
}
Enter fullscreen mode Exit 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.

package main

import "fmt"

func main(){
  fmt.Println(add(2, 4)) //=> 6
}

func add(x, y int) int {
  return x + y
}
Enter fullscreen mode Exit fullscreen mode

Multiple results

A function can return any number of results.

package main

import "fmt"

func main(){
  a, b := multipleArgs("Hello", "World")
  fmt.Println(a, b) //=> World Hello
}

func multipleArgs(arg1, arg2 string)(string, string) {
  return arg2, arg1
}
Enter fullscreen mode Exit fullscreen mode

Variables

The var 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.

package main

import "fmt"

var var1, var2, var3 bool

func main() {
    var num int
    fmt.Println(num, var1, var2, var3) //=> 0 false false false
}
Enter fullscreen mode Exit fullscreen mode

As you can see in the code above, A var statement can be used at package or function level.

Variables with initializers

A var 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.

package main

import "fmt"

func main() {
    var str1 = "ruby"
    var str2, num1, num2 = "go", 2, 4
    fmt.Println(str1,num1, str2, num2) //=>ruby 2 go 4
}
Enter fullscreen mode Exit 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.

package main

import "fmt"

func main() {
    str := "Js"
    num := 4
    boole := true

    fmt.Println(str, num, boole) //=> Js 4 true
}
Enter fullscreen mode Exit fullscreen mode

Basic types

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

bool

string

int  int8  int16  int32  int64
uint uint8 uint16 uint32 uint64 uintptr

byte // alias for uint8

rune // alias for int32
     // represents a Unicode code point

float32 float64

complex64 complex128
Enter fullscreen mode Exit 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.

var i int = 100
var f float64 = i //=> // cannot use i (type int) as type float64
Enter fullscreen mode Exit fullscreen mode

If you want to change the type of a variable, you can do so like the code below.

var a uint32 = 1234567890
var b uint8 = uint8(a)
fmt.Println(b)  // 210
Enter fullscreen mode Exit fullscreen mode

Constants

Constants are declared with the const keyword.

Go supports constants of character, string, boolean, and numeric values.

Constants cannot be declared using the := syntax.

package main

import "fmt"

const Name = "Go"

func main() {
    const Num = 1
    const Truth = true
    fmt.Println(Name, Num, Truth) //=> Go 1 true
}
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
khrome83 profile image
Zane Milakovic

Great intro so far!

Collapse
 
k_penguin_sato profile image
K-Sato

Thank you!