Skip to main content

GoLang from Programming Template

About Memory:

1. How do I get memory, i.e how are variables declared. Any specific facts and constrains about variables ?
Basic data types are here.Declare the variables which you'll need outside the entry point of main function. Something like :
var (
ToBe   bool       = false
MaxInt uint64     = 1<<64 - 1
z      complex128 = cmplx.Sqrt(-5 + 12i)
)
local variables can be declared as shown as example in explanation of FOR loop.

2. How do we get the entry point for the program execution ? [ Example : main() in the C program ]
func main() --> is the entry point of the program execution.

3. How does dynamic memory handled ? Is it supported in first place ?
Yes dynamic memory is supported. new allocates zeroed storage for a new item or type whatever and then returns a pointer to it.Go has garbage collection. This means the Go runtime checks in the background if an object or any other variable is not used anymore and if this is the case, frees the memory.

4. What are the datatypes which are supported and its range?
  • bool
    
    string
    
    int  int8  int16  int32  int64
    uint uint8 uint16 uint32 uint64 uintptr
    
    byte // alias for uint8
    
    rune // alias for int32
         // represents a Unicode code point
    
    float32 float64
    
    complex64 complex128
5. Is user defined datatypes support ? If so how you do it ?
[Reference] [Go's structs are superior to class based inheritance]
In Go, structs are the way to create user-defined concrete types. The design of the struct type is unique compared to other programming languages.
In simple terms, a struct is a collection of fields or properties. Unlike other object-oriented languages, Go does not provide a “class” keyword. Instead, in Go you’ll find structs — a lightweight version of classes. A struct type is exported into other packages if the name of the struct starts with a capital letter. Unlike other object-oriented languages, you don’t need to provide getters and setters on struct fields, as they can access from the same package and can access from other packages as well, if the name of the fields start with a capital letter.

6. How do you cast from one data type to other data type ?
[Related]
[Reference]

_________________________________________________________________________________

About Program Control :

1. What are the program control options available ? [ like "IF ELSE" , "Switch Case ]

Iterative Statements :
[Reference]
go lang has only one iterative statement i.e "for" loop :

package main

import "fmt"

func main() {
sum := 0
for i := 0; i < 10; i++ {
sum += i
}
fmt.Println(sum)
}

Conditional Evaluation Statement :
a. If statement
[Reference]
package main
import "fmt"
func main() {
    if 7%2 == 0 {
        fmt.Println("7 is even")
    } else {
        fmt.Println("7 is odd")
    }
    if 8%4 == 0 {
        fmt.Println("8 is divisible by 4")
    }

    if num := 9; num < 0 {
        fmt.Println(num, "is negative")
    } else if num < 10 {
        fmt.Println(num, "has 1 digit")
    } else {
        fmt.Println(num, "has multiple digits")
    }
}
Here’s a basic example.
You can have an if statement without an else.
A statement can precede conditionals; any variables declared in this statement are available in all branches.

b. Switch statement
[Reference]
  • package main
    
    import (
     "fmt"
     "runtime"
    )
    
    func main() {
     fmt.Print("Go runs on ")
     switch os := runtime.GOOS; os {
     case "darwin":
      fmt.Println("OS X.")
     case "linux":
      fmt.Println("Linux.")
     default:
      // freebsd, openbsd,
      // plan9, windows...
      fmt.Printf("%s.", os)
     }
    }
Go's switch is like the one in C, C++, Java, JavaScript, and PHP, except that Go only runs the selected case, not all the cases that follow. In effect, the break statement that is needed at the end of each case in those languages is provided automatically in Go. Another important difference is that Go's switch cases need not be constants, and the values involved need not be integers.

2. How are Boolean operations supported ?

3. What is the operation precedence applied in compile operation ?
[Reference]


_________________________________________________________________________________

Program refactoring :

1. How do you subprogram ?
package main
import "fmt"

func add(x int, y int) int {
return x + y
}

func main() {
fmt.Println(add(42, 13))
}
2. What are argument passing paradigms supported for subprograms ?

[Reference]
Can be passed by value and pointers as in the reference link.

3. What are the ways in which multi threading applications are created in the programs ?

Can be done by syscall.ForkExec() from the syscall package.
[Reference]

4. How are subprograms called , what are the options we have got ?
Normal calls and call by name as in here.

_________________________________________________________________________________

Comments

Popular posts from this blog

Event Sourcing with CQRS.

  The way event sourcing works with CQRS is to have  part of the application that models updates as writes to an event log or Kafka topic . This is paired with an event handler that subscribes to the Kafka topic, transforms the event (as required) and writes the materialized view to a read store.

GraphQL microservices (GQLMS)

I'm curios of GraphQL !     -  GraphQL is an open-source data query and manipulation language for APIs, and a runtime for fulfilling queries with existing data. GraphQL was developed internally by Facebook in 2012 before being publicly released in 2015. It should be solving a problem in querying data !     -GraphQL lets you ask for what you want in a single query, saving bandwidth and reducing waterfall requests. It also enables clients to request their own unique data specifications. A case study ?!    -https://netflixtechblog.com/beyond-rest-1b76f7c20ef6 So, This is just another database technoloy ?  -  No. GraphQL is often confused with being a database technology. This is a misconception, GraphQL is a   query language   for APIs - not databases. In that sense it’s database agnostic and can be used with any kind of database or even no database at all. Source:   howtographql.com