Getting Started With Basics of Go Variables,Datatypes
Variables
//Ways to declare a vailable
var <variable_name> <type>
var <variable_name> <type> = <value>
var <variable_name> = <value>
var <variable_name1>, <variable_name2> = <value1>, <value2>
<variable_name> := <value> // declare and assign
variable declation in Go using constant
:= declaration will not work for constant in goLang
constant keyword : we can use for surname of a person
Data Types
Types(data types) represent the type of the value stored in a variable, type of the value a function returns, etc.
There are three basic types in Go Language
Numeric types – Represent numeric values which includes integer, floating point, and complex values. Various numeric types are:
int8 – 8 bit signed integers.
int16 – 16 bit signed integers.
int32 – 32 bit signed integers.
int64 – 64 bit signed integers.
uint8 – 8 bit unsigned integers.
uint16 – 16 bit unsigned integers.
uint32 – 32 bit unsigned integers.
uint64 – 64 bit unsigned integers.
float32 – 32 bit floating point numbers.
float64 – 64 bit floating point numbers.
complex64 – has float32 real and imaginary parts.
complex128 – has float32 real and imaginary parts.
String types – Represents a sequence of bytes(characters). You can do various operations on strings like string concatenation, extracting substring, etc
Boolean types – Represents 2 values, either true or false.
Arrays :
var arrayname [size] type
arrayname [index] = value
arrayname := [size] type {value_0,value_1,…,value_size-1}
You can also ignore the size parameter while declaring the array with values by replacing size with … and the compiler will find the length from the number of values. Syntax is
arrayname := […] type {value_0,value_1,…,value_size-1}
Loops : For loop If , If else , if else if else switch switch is another conditional statement. Switch statements evaluate an expression and the result is compared against a set of available values(cases). Once a match is found the statements associated with that match(case) is executed. If no match is found nothing will be executed. You can also add a default case to switch which will be executed if no other matches are found
switch expression {
case value_1:
statements_1
case value_2:
statements_2
case value_n:
statements_n
default:
statements_default
}
FUNCTION : "function signature".
func sub(x int, y int) int{
}
********OR*******
func add(x, y int) int {
return x + y
}
func main() {
a := 10
b := 5
mul, div, err := calculator(a, b)
fmt.Println(mul, div, err)
}
// Named return parameters are particularly important in longer functions
//with many return values.
func calculator(a, b int) (mul, div int, err error) {
if b == 0 {
return 0, 0, errors.New("Can't divide by zero")
}
mul = a * b
div = a / b
return
}
Golang Interface is a collection of method signatures used by a Type to implement the behavior of objects. The main goal of Golang interface is to provide method signatures with names, arguments, and return types. It is up to a Type to declare and implement the method. An interface in Golang can be declared using the keyword “interface.”
**Quick bite : **Until recently the GOPATH setup allowed developers to import packages as long as they were in the local Go workspace. As of Go ~1.13 it’s recommended to use go mod. With go mod, source code isn’t required to be a part of the GOPATH environment variable.
Go doesn’t use a package manager like NPM or Cargo. The Go toolchain provides commands like go get for fetching external dependencies straight from their remote source control repositories, and go mod for managing the dependencies of a specific project.
Go provides built-in channels for go-routines to communicate safely between themselves. More goroutines can run on a typical system than system threads. For example, with Java, you can run many thousands of threads. With Go, you can run many millions of goroutines. Goroutines startup more quickly than operating system threads. Multiple Goroutines are multiplexed onto OS threads, rather than a 1:1 mapping. You can write massively concurrent servers without having to resort to event programming. Goroutines are not hardware-dependent like threads. Goroutines are more lightweight, largely due to segmented stacks in memory
Use SQLite or PostgresQL instead of a JSON file for your database layer Let users save other data with their posts, maybe an image URL? Add proper authentication to each request, you could even use my password validator Add more unit tests! Deploy your api on AWS, GCP, or Digital Ocean Dockerize it Add documentation using markdown files Write a client that interacts with the API, maybe a webpage or a mobile app