Skip to content

This repository contains learning material and "how to dos" in GoLang

Notifications You must be signed in to change notification settings

Anshul619/Golang

Repository files navigation

What is Golang?

Key Features

Title Remarks
Types in GoLang Type parameters permit what is known as generic programming, in which functions and data structures are defined in terms of types that are specified later, when those functions and data structures are used.
Coding Helpers & Guidelines in GoLang Coding Helpers and guidelines for coding in GoLang.
Pointers in GoLang GoLang supports pointers using (*, & operators)
Concurrency in GoLang Go provides very good support for concurrency using Go Routines or channels
Slices in GoLang Slice in Go is a lightweight data structure of variable length sequence for storing homogeneous data.
OOPs in GoLang Although Go has types and methods and allows an object-oriented style of programming, there is no type hierarchy.
Panic & Recover in GoLang Panic & Recover is like exception in GoLang.
Unit Testing in GoLang GoLang supports unit testing using Testing package
Labels in GoLang Label is used in break and continue statement where it’s optional but it’s required in goto statement.
How to Work With SQL in GoLang? database/sql package helps to query SQL databases.
DB Transaction in GoLang Using Begin, Commit code block, atomicity can be implemented in GoLang.
Packages In go, code is organized using packages.
Modules Modules represent an app/service in Go.
Comments & Documentation in Go -

What do you understand by Golang string literals?

  • String literals are those variables storing string constants that can be a single character or that can be obtained as a result of the concatenation of a sequence of characters.
  • Go provides two types of string literals - Raw & Interpreted string literals.

Raw string literals

`interviewbit`

Interpreted string literals

"Interviewbit
Website"

What is the syntax used for the for loop in Golang?

for [condition |  ( init; condition; increment ) | Range]  
{  
statement(s);  
//more statements
}  

What do you understand by the scope of variables in Go?

Local variables

  • These are declared inside a function or a block and is accessible only within these entities.

Global variables

  • These are declared outside function or block and is accessible by the whole source file.

How can we check if the Go map contains a key?

img.png

if val, isExists := map_obj["foo"]; isExists {
    //do steps needed here
}

Can you format a string without printing?

return fmt.Sprintf ("Size: %d MB.", 50)

Closures in Golang

// Golang program to illustrate how
// to create a Closure
package main

import "fmt"

func main() {
	
	// Declaring the variable
	GFG := 0

	// Assigning an anonymous
	// function to a variable
	counter := func() int {
	GFG += 1
	return GFG
	}

	fmt.Println(counter())
	fmt.Println(counter())	
}

Scan Function in GoLang

  • The Scan function in the Go programming language is used to read data from the standard input, format the string, and store the resultant strings into the destinations specified by the additional arguments.
package main
 
import (
    "fmt"
)
 
func main() {
 
  var name string
  var unit string
  var amount int
  var temp string

  // taking input and storing in variable using a sample input string would be:
  // "Faraz owns 500 acres of land"
  fmt.Scan(&name, &temp, &amount, &unit)
 
  // print out new string using the extracted values 
  fmt.Printf ("% d %s of land is owned by %s\n",amount, unit, name);
}

Format the string

  • In Go, the %s verb is used to format a string.
  • When used with a custom type that has a String() method defined, the String() method will be automatically called and its return value will be used in the formatted string.

Variadic Functions in Go

  • The function that is called with the varying number of arguments is known as variadic function.
function function_name(para1, para2...type)type {// code...}

Switch

  • One interesting thing about switch statements, is that the value after the switch keyword can be omitted, and we can have boolean conditions for each case.
age := 21

switch {
case age > 20 && age < 30:
    // do something if age is between 20 and 30
case age == 10:
    // do something if age is equal to 10
default:
    // do something else for every other case
}

References

About

This repository contains learning material and "how to dos" in GoLang

Topics

Resources

Stars

Watchers

Forks

Languages