in go google programming ~ read.

Introduction of Go

In this post we are going to talk about Go, an open source programming language that makes it easy to build simple, reliable, and efficient software.

What is Go?

Go, a programming language created by Google in 2007.

Why Go?

It utilises the goodness from C/C++ and provide much more elegant way of writing software. Some big ass companies like Docker are using Go.

Setup

All of the installation process should be the same as their official page. Head over to their website and following the instructions. I will be waiting here I promise.

Variables & Constants

Let's have a look at the following syntax and analyse it as we go.

package main

import ("fmt")

var (  
    name     string
    course   string
)

func main() {  
    fmt.PrintIn("Name: ", name)
    fmt.PrintIn("Course: ", course)
}

There are some difference that you need to take care of as the following:

package main - As you can image that it is like the namespace in PHP import ("fmt") - It is using other package called fmt var(...) - Go uses var keyword if it is declared at Package level. You don't need to use var if you declare your variables in func level.

Determining Types

Go could either be statically-typed or dynamically typed language based on the way you create your variable. Let's look at how to determine types in Go as following:

package main

import ("fmt", "reflect")

var (  
    name     string
    course   string
)

func main() {  
    fmt.PrintIn("Name: ", name, "Type of: ", reflect.TypeOf(name))
    fmt.PrintIn("Course: ", course, "Type of: ", reflect.TypeOf(course))
}

Short Assignment

Like I mentioned before, you dont need to use var keyword if you declare variables within func. Let's look at the following.

package main

import ("fmt", "reflect")

func main() {

    name   := "Well"
    course := "Programming"

    fmt.PrintIn("Name: ", name, "Type of: ", reflect.TypeOf(name))
    fmt.PrintIn("Course: ", course, "Type of: ", reflect.TypeOf(course))
}

As you can see, there is no ; at the end of the syntax which I think is pretty awesome. Keep the code clean.

Passing by value

We can choose either passing variables using value or reference. Let's have a look at the following of passing variables using value.

package main

import ("fmt", "reflect")

func main() {

    name   := "Well"
    course := "Programming"

    fmt.PrintIn("Name: ", name, "Type of: ", reflect.TypeOf(name))
    fmt.PrintIn("Course: ", course, "Type of: ", reflect.TypeOf(course))

    changeCourse(course)
}


func changeCourse(course string) string {

    course = "Howdy"

    fmt.PrintIn(course)

    return course
}

Passing by reference

Next let's look at the example passing by reference

package main

import ("fmt", "reflect")

func main() {

    name   := "Well"
    course := "Programming"

    fmt.PrintIn("Name: ", name, "Type of: ", reflect.TypeOf(name))
    fmt.PrintIn("Course: ", course, "Type of: ", reflect.TypeOf(course))

    changeCourse(course)
}


func changeCourse(course string) string {

    course = "Howdy"

    fmt.PrintIn(course)

    return course
}

Constants

The way to create a constant is pretty normal.

const daysOfAYear = 365  

Functions

As you see before that the way to create a function is like the following.

package main

import ("fmt", "strings")

func main() {  
    name     := "Well"
    course   := "Programming"

    fmt.PrintIn(strings.ToUpper(name))
}

Conditional

If

If statement in Go is pretty normal compared to others. Let's look at the following example.

package main

import ("fmt")

func main() {  
    num1 := 20
    num2 := 28

    if num1 > num2 {
        fmt.PrintIn("Num1 is bigger then Num2")
    }
}

Notice the difference that there is no () then we asserting num1 is bigger then num2.

Switch

Let's look at the following switch statement.

package main

import ("fmt")

func main() {

    switch "well" {
    case "well":
        fmt.PrintIn("It is Well")
    case "joey":
        fmt.PrintIn("It is Joey")
    case "ross":
        fmt.PrintIn("It is Ross")
    case "chandler":
        fmt.PrintIn("It is Chandler")
    default:
        fmt.PrintIn("It is someone I dont know")
    }
}

For

package main

import ("fmt")

func main() {

    for timer := 10; timer >= 0; timer-- {
        if timer == 0
            fmt.PrintIn("Done")
            break
    }

}

Array & Slice

Let's look at the following code and run through them carefully.

package main

import ("fmt")

func main() {

    names := make([]string, 5, 10)
    fmt.Printf("Length is: %d and Capacity is: %d, len(names), cap(names)
}

In the above, we use make key to define array and gives it a type of string and values of 5 and 10.

Map

Map is a collection of object key value pair. Like the associative array in PHP or object in JavaScript. Let's look at the following code.

package main

import ("fmt")

func main() {

    names := map[string]int {
        "firstName": "Well",
        "lastName": "Lin"
    }

    fmt.Printf(names)

}

Structs

And you can also use structs to create your own data type as well. Have a look at the following example.

package main

import ("fmt")

func main() {

    type person struct {
        FirstName string
        LastName  string
    }

    Well := person(
        FirstName: "Well",
        LastName: "Lin"
    )

}

End

This is a rather simple and short introduction article about Go. Hope this article will open your first step to try out Go in your development. As always if you have any opinions or ideas please leave a comment below. Thanks!

comments powered by Disqus