Git Product home page Git Product logo

go-practice's Introduction

Go Practice

Table of Contents

Roadmap

Specific Things

Notes

  • len() returns the byte size of a string. So if char is not in ASCII table it could stored as more than one byte.
  • If you don't specify the length of the array, it will be a slice
  • Use make() to initialize things with declaring capacity
  • Go has only one looping construct, the for loop
    • But for loop can be used as "regular for loop", "while", or "foreach"
  • Map always returns value even if the key doesn't exist
    • But it returns a second value to indicate if key exists.
  • Switch cases can be used with or without variables. If variable isn't passed, it can be used like if branch.
  • Functions can return more than one value

Tip

Don't forget to use pass by reference when passing large arrays.

  • You can also make in-line structs that can be used one time.
  • Interfaces is similar to abstract classes in C++
  • Pointers are exactly like C
  • Select-case statement is if statements for channels
  • See testing package docs.
    • Test files naming convention need to be snake-case
    • Test function naming convetion need to be pascal-case

Important

Exception for copying values without pointers is slices.

Because slices use pointers to address values and if you copy a slice, you copy the addresses.



Specific Things

Keyword defer

  • A defer statement defers the execution of a function until the surrounding function returns

Goroutines

  • concurrency != parallel execution

  • For example:

    • Program can execute other processes while waiting for database to respond
  • Device need to have a multicore CPU to execute processes in parallel

  • Usage

    • use go keyword to use goroutines (async functions)
    • import sync
      • use WaitGroup: a counter for tasks
      • use wg.Add(1) before using async method to increment wait group
      • use wg.Done() to indicate the task is ended and decrease the counter
      • use wg.Wait() to wait till wait group has ended so the rest of the program can continue executing
  • Example code

    • Example implemented here

Tip

Check sync.Mutex and Lock()-Unlock() methods to prevent accessing the same memory at the same time

go-practice's People

Contributors

alperencode avatar

Watchers

 avatar

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.