Monday, 28 August 2017

3 - Go Simple and Complex Variables

In last small sessions…

  • we
    • Had a small look on different IDEs (among-st them, gogland was the one which suits my requirements)
    • Had an overview of program structure in golang
    • Had a small description on which strategy should be followed for naming conventions for coding
  • There is a blog which has all the standards mentioned… Link is provided in description:

    Quick Recap

    • Go files are stored with “.go” extension.
    • There are two environment values which are required to run go-program
      • GOROOT: The place where we will install golang (for example, C:\go)
      • GOPATH: The place where we will work (for example, D:\workspace)
    • Under GOPATH, we’ll have following structure
      • GOPATH
        • Src (This is where we will keep our work)
        • Pkg (This is where go-framework will pull the package required for our work)
        • Bin (This is where golang generates the final executable)
    • Program structure: basic is
      • Package name (mostly it is same as directory name)
      • Import section
      • Functions
      • (The structure will grow along with our tutorial)
    • “package main” is the starting package and “func main()” is the place where golang program starts the execution
    • To build (compile) the golang program, first configure the environment variables
    • Then open command prompt, go to working directory, and then use
      • “go install”
    • It’ll pull all the required packages to pkg folder and create an executable in bin folder

    Go-Variables

    • In golang, variables can be declared in 2 ways.
    • Simple reference
      • var intval int
      • This variable will not have any memory. They are just references
    • Direct assignment with allocation
      • intval := 2
      • This statement will declare an int type variable (type will be derived from right side of the operator), it will be allocated a memory and value 2 will be stored there
      • Personally, I call it 3-in-1 functionality…

    Structures

    • For complex type of objects, golang provides the structures.
    • If we want to represent any real world entity, then we need to describe the properties. Like,
      • That car is red color
      • It is produced by Hundai
      • It is EON model
      • ETC...
    • In such cases, we need different properties like 
      • Color
      • Company
      • Model
      • Etc...
    • golang provides the facility to represent such objects by structure. We can create basic template as given in example.

    • In our case, structure will have 4 properties (you can add more as per your requirements).
    • Now, creating an object is similar to create a variable
    • You can create a reference or you can directly assign a new memory space along with reference creation.

    Hence, Simple and Complex Variables

    • According to golang syntax and standards,
    • Local variables and structures should start with lowercase letter
    • The names should be short, meaning full and self-explanatory
      • For example,
        • var i int (i = index)
        • var ds departmentalstore (ds = departmental store)
        • var fc foreigncurrency (fc = foreign currency)
    • Go-compiler doesn’t allow you to create unused variables, that way, it tries to avoid un-necessary memory leaks
    • Err variables should not be re-created at each iteration. Try to reuse it.








      Sunday, 27 August 2017

      2. IDE for golang

      IDE (Integrated Development Environment)

      • If you’re a developer, then you must be aware of the need.
      • IDE creates an environment for easy development.
      • In last session, we developed the sample program on notepad.
      • IDE provides many of intellisense options, easy builds and execution facility
      • It also provides a debugging option (so that, how program will react at the real run-time can be observed)


      Go Language IDEs

      • For Go-language, there are plenty of IDEs, For example:
        • VSCode
        • Goeclipese
        • Intellij
        • Gogland
        • Sublime text
        • Atom
        • Zeus
        • Netbeans
        • Etc...
      • Apart from this, there are some editors which provides text base environments like vim and evacs.

      • According to my research and comparison, (which would be difficult to present here…), we have below mentioned list of IDEs from most features to less comfort ability for IDEs. (I am presenting this work on August 2017).
        • Gogland
          • Jetbrain’s product
          • intellisense
          • Auto-code analysis
          • compilation
          • debugging
          • test case execution
          • test case debugging
          • repository integrations, etc.

        • VSCode (after 2015 release)
          • Microsoft product
          • little less intellisense
          • No code analyzer
          • compilation
          • debugging, etc.
        • Sublime text
          • first and famous IDE for golang
          • now a days, it comes with many facilities
          • still lacking test case debugging
          • No Automatic code analysis
        • LiteIDE
          • Easy to use
          • compatible for golang
          • compilation feature came very late
          • test case debugging is not available
        • Intellij
          • test case debugging is not available
          • However, code analysis is available at run-time
          • Code coverage can be generated
        • Eclipse, Netbeans
          • favorite for Java developers
          • to configure, additional step of import go-plugin is required
          • code analyzer is not available 
        • Intype, Kombo Edit…. ETC.
      • I am working on windows-7 and I have installed gogland…. J
      you guys, can change the IDEs as per your requirements.

      1 - Introduction

      Why GO?

      • For a simpler reason, because it’s new, free and open-source.
      • Most of the time my boss asks me to work on new technology when it’s not new to market and others are praising it… (then I give him the advantages and dis-advantages if we use that)
      • Let me list down few benefits
        • Crisp
        • Clean and effective
        • Fast
        • Awesome con-currency mechanism
        • And many more...


      Attraction

      • Main attraction : Open-source
      • Performance wise, it utilize the maximum capacity of hardware
      • Interface mechanism
      • Reflection
      • Fast compilation and Quick conversion to machine code
      • Type system : awesome
      • Neat and modular
      • ETC... ETC... ETC...

      Let's start learning Go Language (golang)

      • To begin with GO, first of all you need to have Go framework installed on your computer.
      • Visit - https://golang.org/dl/
      • Download the version best suited for your application
      • At the time of installation, just note down the path where you are installing it
      • In installation, it is just un-zipping the framework files. Go ahead an take look, open any file (its open-source…)

      Configuration...

      • Go compiler uses two environment variable
      • GOROOT : set the value where you installed your go framework
      • GOPATH : set the value where you are working
      • For example, in our windows systems, we used to install it in
        • C:\Go folder and work in D:\Go folder so,
          • GOROOT = C:\Go
          • GOPATH = D:\Go
      • That’s all… Our primary set-up for Go-Language is done

      First Small Run...


      • Let’s make a basic, “Hello World.” console application.
      • Open a notepad and write below mentioned things in there.
      • Save it as helloworld.go in (GOPATH)/Src/ folder

      • In our case, it'll be "D:\Go\Src\helloworld.go"

      Try Getting Output...

      • Open the command prompt and browse to that directory where this file is stored (and also configured as GOPATH)
      • Now, run bellow mentioned command
        • go install
      • It will compile and create an executable file in bin folder (created as a sibling to Src folder)
      • Try executing this file and you’ll get "Hello, world."





      4 - Go Control Statements

      In last small sessions… we Learnt how to create simple variables The syntax to create structures Create objects of structure types a...