GithubHelp home page GithubHelp logo

learngo's Introduction

Learn Go

Learning Go

This is a ReadMe template to learn basic syntax of Golang.


Table of Contents

You're sections headers will be used to reference location of destination.


Description

Creating ReadMe's for your Github repository can be tedious. I hope this template can save you time and effort as well as provide you with some consistency across your projects.

Technologies

  • Technology 1
  • Technology 2

Back To The Top


How To Use

Basic Syntax

  1. string(bs) -> converts Byte Slice to the String type

  2. strings.split(s, separator string) -> String.split(String s,char c)

  3. rand.Intn(i int) -> Random.rand(Integer i)

  4. fmt.Printf("%+v", struct) -> Prints the value of each field in the struct followed by actual value

  5. Value Types -> int, float, string, bool, structs (we use pointers to change these things in a function)

  6. Reference Types -> slice, maps, channels, pointers, functions. Don't worry about pointers with these.

  7. Make Function -> bs = make([] byte, 9999) - makes a byte slice with 9999 elements in it.

  8. io.Copy -> io.Copy(os.Stdout, resp.Body)

Installation

API Reference

    <p>dummy code</p>

Back To The Top


References

Back To The Top


Error Handling

Back To The Top


Unit Tet in Go

Unit Test in Go

  • To make a test, create a new file ending in _test.go like deck_test.go
  • To run all the tests in a package, run the command
    go test

Back To The Top


Struct in Go

  • Data Structure, Collection of properties that are related.
  • Declaring a struct
type person struct {
    firstName string
    lastName string
}

func main() {
    alex := person{firstName: Alex, lastName: Anderson}
    fmt.Println(alex)
}

More Example of Struct

type contactInfo struct {
	email   string
	zipCode int
}

type person struct {
	firstName string
	lastName  string
	contactInfo
}

func main() {
	jim := person{
		firstName: "Jim",
		lastName:  "Party",
		contactInfo: contactInfo{
			email:   "[email protected]",
			zipCode: 94000,
		},
	}
	fmt.Printf("%+v", jim)
}

Back To The Top


Pointers in Go

  • &variable -> (& is an operator) Give me the memory address of the value this variable is pointing at.
  • *pointer -> (* is an operator) Give me the value that this memory addresss is pointing to.
  • *person -> (person is a struct type) Type description - we are working with a pointer to a person.

Turn address into value using *address.
Turn value into adderss using &value.
Pointer shortcut is to call a function expecting the pointer to a type using the type only.

    func (pointerToPerson *person) updateName() {
        (*pointerToPerson).firstname = "jimmy"
    }

Pointer Gotcha

The below code will output [Bye There How Are You] which is different than expectation, but behaviour of Pointers is different for slice and struct.

    func main() {
        mySlice := []string{"Hi", "There", "How", "Are", "You"}
        updateSlice(mySlice)
        fmt.Println(mySlice)
    }

    func updateSlice(s []string) {
        s[0] = "Bye"
    }

Slice internally stores the elements in an array. Slice has three components (ptr to head of an array, capacity, length). Hence even when there is a copy of slice the underlying array is the same.

slice is referred to as Reference type.

Back To The Top


Map in Go

Map stores key, value pairs, all keys and all values should be of same type respectively

Back To The Top


Interface in Go

Declaring an interface type in a file, says our program has a new type called bot

    type bot interface {
	getGreeting() string
}
type englishBot struct{}
type spanishBot struct{}

func main() {
	eb := englishBot{}
	sb := spanishBot{}

	printGreeting(eb)
	printGreeting(sb)
}

func printGreeting(b bot) {
	fmt.Println(b.getGreeting())
}
func (englishBot) getGreeting() string {
	return "Hi There!"
}

func (spanishBot) getGreeting() string {
	return "Hola!"
}

If you are a type in this program with a function called 'getGreeting' and you return a string then you are a member of type 'bot'

Now that you are an honorary member of type 'bot' you can now call this function called printGreeting()

  • Interfaces are not generic types and Go does not have support for the same.
  • Interfaces are implicit, as in java we don't manually have to say that our custom type satisfies some interface
  • Interfaces are a contract to help us manage types. if our custom type implementation is broken interfaces won't help.
  • Interfaces are tough. Step #1 is understanding to read them. Writing our own interfaces is tough and requires experience.
  • We can take different interfaces and combine them together to form a new interface.
    type ReadCloser interface {
        Reader 
        Closer
    }

    type Reader interface {
        read([]byte) (int, error)
    }

Back To The Top


Go Routines

  1. Our running program (a process) --

Author Info

Back To The Top

learngo's People

Contributors

kapilgupta101292 avatar

Watchers

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