Giter VIP home page Giter VIP logo

go_learning_repo's Introduction

Go_Learning_Repo

This is my Go Learnig Repository for all fellow Go noobs. Focused on InfoSec.

Why Go ?

  • Cross Platform
  • Fast
  • Easy to write (lets see)

How to run and compile Go ?

$go run hello.go

will execute the current programm without needing to compile it

To create a binary we need this command:

$go build hello.go

Go binaries are quite big by default we can use these parameters to make them smaller:

$go build -ldflags "-w -s"

How do we cross compile in Go ?

This command will build an executable for linux 64 bit systems:

$GOOS="linux" GOARCH="amd64" go build hello.go

Nice !

Other useful Go Commands

  • The "go doc" Command will show you Guidance Papers on any part of Go ($go doc fmt.Println )
  • The "go get" Command is an equivalent to python pip
  • The "go fmt" Command will format your code for you, by enforcing the use of proper line breaks, indentation and brace allignment
  • Last but not least the "go vet" Command which will do some heuristic stuff on your code and tell you why your code suckx :)

Go Syntax

Primitive Data Types

primitive types include: bool, string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr, byte, rune, float32, float64, complex64, complex128

Declaring a variable:

var x = "Hello World"
or
y := "Hello World"

var x = int(42)
or 
y := int(42)

Slices and Maps

Go also has more complex data types like slices and maps. Slices are like arrays that you can dynamically resize and pass to fucntions. Maps are associative arrays, unordered lists of key/value pairs that lets you quickly look up values for a unique key.

Example of a Slice:

var s = make([]string, 0)
s = append(s, "hek")

Example of a Map:

var m = make(map[string]string)
m["some key"] = "some value"

Pointers, Structs and Interfaces

Pointers in Go work like they do in C. We use the &-operator to retrieve the memory address of some variable, and the * -operator to dereference the address.

Pointer Example:

var count = int(42)
var ptr = &count
fmt.Println(ptr)  //prints memory address of count
fmt.Println(*ptr) //prints the value of count
*ptr = 100        //writes new value into count
fmt.Println(count)

Struct Example:

type Person struct {
  Name string
  Age int
}
func (p *Person) SayHello() {
  fmt.Println("Hello,", p.Name)
}
func main() {
  var guy = new (Person)
  guy.Name = "Dave"
  guy.Age = 21
  guy.SayHello()
}

Example of a interface:

type Friend interface {
  SayHello()
}

func Greet (f Friend) {
  f.SayHello()
}

func main() {
  var guy = new (Person)
  guy.Name = "Gary"
  Greet(guy)

Control Structures

Basic If-else Statement

if z == 1 {
		fmt.Println("Z is equal to 1")
} else {
		fmt.Println("Z is not equal to 1")
}

Basic Switch Statement

var x = "foo"
switch x {
	case "foo":
		fmt.Println("found foo")
	case "bar":
		fmt.Println("Found bar")
	default:
		fmt.Println("Default case")
}

Go doesnt hava a do or while loop. Only a for loop. For Loop Example:

for i := 0; i < 10; i++ {
  fmt.Println(i)
}

Concurrency - Goroutines

To exeucte code concurrently we can use goroutines. Some people call them lightweight threads, because the cost of creating then is minimal.

Example:

func f() {
	fmt.Println("f function")
}

func main() {
	go f()
	time.Sleep(1*time.Second)
	fmt.Println("main function")
}

Error Handling

type error interface {
	Error() string
}

//Custom Error Message
type myError string
func (e myError) Error() string {
	return string(e)
}

Error handling pattern:

func foo() error {
	return errors.New("Some Error Occured")
}

func main() {
	if err := foo(); err != nil {
	//handle the error
	}
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.