Giter Site home page Giter Site logo

effectivego's Introduction

Effective Go

  • Code samples coming out of studying Effective Go
  • Some notes are collected in this document

General

  • Things created with make are reference types

Assignment

  • Normally vars can only be declared once in scope

  • For multi-assigments (i.e. func cal returns multiple values) some extra rules apply:

    res, err := foo() // this is fine
    
    res2, err := foo2() // this also fine, res2 is a new var
    
    res, err := foo2() // this is not fine, no new vars introduced
    
    res, err = foo3() // this is fine
    
    if err != nil {
        res, err := foo2() // also fine, new block means new scope, res is redeclared
    }

Runes

  • Runes represent multi-byte characters, i.e. the ones used by utf-8

  • utf-8 codepoints use up to 4 bytes, so rune is actually an alias for int32

  • A range over a string iterates over its runes:

    str := "abc日本\x80語"
    
    for _, r := range str {
        fmt.Printf("%T %#U", r, r) // something like "int32 U+65E5 '日'"
    }

Type Switching and Casting

  • Type switches looks like this:

    // t will have dynamic type of thing
    switch t := thing.(type) {
    case string:
        fmt.Println("it is a string")
    default:
        fmt.Println("unknown type")
    }
  • Type assertions use an actual type inside the parens: t := thing.(string)

  • If type doesn't match, a runtime error will be thrown, or use "comma, ok" idiom:

    if t, ok := thing.(string); ok {
        fmt.Println("it's a string")
    }

effectivego's People

Contributors

joerx avatar

Watchers

James Cloos 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.