27
The Go Programming Language By İbrahim Kürce Quoted from John Sonmez Three chapters: Go Overview, Go Development and Variables, Types, Pointers

The Go Programing Language 1

Embed Size (px)

Citation preview

The GoProgramming LanguageBy İbrahim Kürce

Quoted from John Sonmez

Three chapters: Go Overview, Go Development and Variables, Types, Pointers

1 – Go Overview – What is Go?

Compiled

Garbage-collected

Concurrent

Compiled

Code adapted to machine that they’re running on

Compiled down to machine languages like C, C++

More efficient and better performance than interpreted languages

Supported operating systems: Linux, Mac OS X, FreeBSD, OpenBSD, Plan 9, Windows

Supported processors: i386, amd64, ARM

Garbage-collected

You do not have to manage memory as programmer, unlike C, C++

Runtime handles for you

Garbage collection is very fast, latency free

Concurrent

Concurrency is to do more than one thing at a time

Programming languages like C++ and Java, concurrency is possible but hard tomanage. It is not part of language

Go has built in concurrency support

It is called Go Routines for concurrent functions

Go’s Origins

Three Google engineers want a system level languages for aging systems levellanguages

Go was designed very fast compiling language

Sept 2007Dream of Go

March 2012Go 1

released

May 2010Used

internally at Google

Nov 2009Officially

announced

What makes Go different?

Efficient like a static language, ease of use like dynamic language

Type-safe and memory-safe

Latency free garbage collection

Fast compile times

It does not have assertions and method overloading

2 – Go Development - Packages

Way to modularize code

Similar to namespaces

Collection of types and functions

Can import packages in your code

Redistribute those packages

A lot of built-in packages, https://golang.org/pkg/

Imports

Code.go

Source code

Local packages

Remote packages

Imports

Go knows whether your code needs those packages

Has ability to go out and get remote packages

Your code could refer to github etc. repositories

Hello World

package main Required package name "main"

import "fmt" Package comes with Go

func main(){ Curly braces is near by function name

fmt.Println("Hello World")

}

Println start with upper case, cause it is exported method

Hello World

Direct run, not generate .exe file

go run hello.go

To Build to generate .exe file

go build hello.go

To run after build

hello.exe

3 – Variables, Types and Pointers

Basic Types

bool

string

int, int8, int16, int32, int64

uint, uint8, uint16, uint32, uint64, uintptr

byte(unit8)

rune(int32), like a char

float32, float64

complex64, complex128

Types

Other types

Array

Slice

Struct

Pointer

Function

Interface

Map

Channel

Basic Declaration

Multi-Declaration and Initialization

Multi-Declaration and Initialization

Default value is 0 for int types

Default value is always set in Go language

Without Types Without var

Multi-Declaration and Initialization

Do not need to have same type in multi-declaration

Pointer Basics

Pointer is a type of variable that is going to contain a memory address of anothervariable

string pointer has same type with string type

HelloWorld

0xc04200a270

Pointer Basics, Passing Things

Pointer’s type is that type is going to refer to what type of variable

We pass value in Go function, this value will be copied, inside function, we changethis value, originial value is not changed

function

4343

function

4345

Passing Things

If we pass a pointer to function, pointer also will be copied but it points samememory location. So if we change value inside function, originial value will also be changed

function

430x0123

0x0123

Basic Pointer Example

greeting is a pointer, if we write it directly, it shows memory address

If we write *greeting, it shows Hello world

Pointer Assignment

If we change value of pointer, we also change originial value

No Classes in Go

Go is not object-oriented language

Instead of using classes in Go, we use what’s called user define types

For example;

Basic User Types

Salutation struct definition

Constants

In Go, you can define constants without defining the type

PI is numeric, Language is string type

A, B, C = 0, 1, 2 respectively

End of Go Overview, Go Development andVariables, Types, Pointers