Basics types in Go

strings, integers, floats, booleans etc

Add operation on strings concatenate, multiplication on strings multiplicate how

Mathematical operations on int and float produce equivalent output with upcast (increasing in the degree of type)

and / or / not on boolean returns boolean

Values

package main

import (
	"fmt"
)


func main() {
	fmt.Println("hello world")
	// strings
	fmt.Println("hello" + "world")
	// int
	fmt.Printf("the int sum is %d", 1 + 2) //formatted string
	// float
	fmt.Println(1.2 + 23)
	// boolean
	fmt.Println(true && false)
	
}

Variables

package main

import (
	"fmt"
)


func main() {

	// variables
	var f int // change also dynamically, redeclaring it will cause error
	f = 4 // int
	f = 43
	fmt.Println(f)
	
	var g int = 4 
	fmt.Println(g)
	
	d := "string" // declare and initialize but d "alone" cannot be used second time
	fmt.Println(d)
	
	//d,_ := 23 // will cause no new variable
	//fmt.Println(d)
	
	/*
	// read below
	d,t := 23,12
	fmt.Println(d,t)
	*/
	
	// once you have declared the same variable using := you can reassign without changing its type, above examples does not run unless you remove 
	// previous declaration of d
	
	// other way of declaring
	var a,v,t int = 1,2,3
	fmt.Println(a,v,t)
	
}

Constants

package main

import (
	"fmt"
	"math"
)


func main() {
	
	// constants, string, boolean and numeric values
	
	const s = 23
	fmt.Println(s)
	_ = s
	
	// use of const
	const pi = 3.14
	fmt.Println(math.Sin(pi))
	
	}

Last updated

Was this helpful?