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).
- 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.



No comments:
Post a Comment