Golang Aesthetic Notes #2
Links to Basics:
https://go.dev/doc/tutorial/getting-started
Format Verbs:
Symbols used in print commands such as %v or %g or %#g are to embed the values of variables of any data type between the characters in a string.
These symbols are called Format Verbs.
Different symbols are for different data types. Read more on them here.
For e.g.,
fmt.Printf("Now you have %g problems.\n", math.Sqrt(7))
Output:
Now you have 2.6457513110645907 problems.
clarifying the usage %g and %f:
References: quora and stackoverflow
From what I can read from people's explanations, %g is supposed to be used where using %e or %f would output too big a numerical expression. So, in essence, %g puts to a concise form, extremely exponential or decimal values.
Writing Functions:
An important aspect of writing functions in Golang is the data type declaration for both input parameters and the return values.
func add(x int, y int) int {
?? ?return x + y
}
Within the brackets following the name of the function, the parameter data types are defined, followed by the definition of the return data types.
Named Return Values:
This is when at the bottom of the function, the command return is used without being followed up with any output variables. They call this a naked return.
When you use a naked return, simply all the output variables will be returned. There's more to know, so please check out the reference linked above.
package main
import "fmt"
func split(sum int) (x, y int) {
?? ?x = sum * 4 / 9
?? ?y = sum - x
?? ?return
}
func main() {
?? ?fmt.Println(split(17))
}
//Output: 7 10
Variables:
If an initializer is present, the type can be omitted; the variable will take the type of the initializer.
This means that if the variable i is initialized in the following manner:
领英推荐
var i = 132
Then, there's no need to declare the datatype for i. Go will identify it as an integer since it has been initialized with an integer value.
To initialize local variables:
Use the " := " statement.
package main
import "fmt"
func main() {
?? ?var i, j int = 1, 2
?? ?k := 3
?? ?c, python, java := true, false, "no!"
?? ?fmt.Println(i, j, k, c, python, java)
}
Basic Types:
source: geeksforgeeks
int8 8-bit signed integer
int16 16-bit signed integer
int32 32-bit signed integer
int64 64-bit signed integer
uint8 8-bit unsigned integer
uint16 16-bit unsigned integer
uint32 32-bit unsigned integer
uint64 64-bit unsigned integer
int Both int and uint contain same size, either 32 or 64 bit.
uint Both int and uint contain same size, either 32 or 64 bit.
rune It is a synonym of int32 and also represent Unicode
code points.
byte It is a synonym of uint8.
uintptr It is an unsigned integer type. Its width is not defined,
but its can hold all the bits of a pointer value.
In Integers are of types, signed and unsigned. The difference between the two are the following:
References:
And as you might have probably guessed the number of bits used to represent an integer decides the biggest integer that can be represented. Read more, here.
While trying to understand the statements used in the sample code, there was unfamiliar kind of expression used. Check this out:
package main
import (
?? ?"fmt"
?? ?"math/cmplx"
)
var (
?? ?ToBe?? bool?????? = false
?? ?MaxInt uint64???? = 1<<64 - 1
?? ?z????? complex128 = cmplx.Sqrt(-5 + 12i)
)
func main() {
?? ?fmt.Printf("Type: %T Value: %v\n", ToBe, ToBe)
?? ?fmt.Printf("Type: %T Value: %v\n", MaxInt, MaxInt)
?? ?fmt.Printf("Type: %T Value: %v\n", z, z)
}
wth is 1<<64 -1 ??
Then after bit of reading online I came to know that << or >> is used in shifting bits to a specific position of exponential power. This geeksforgeeks link explains about left shift and right shift operators in general.
This is a link to a gem of stackoverflow thread that explains the meaning for 1<<64 -1. If none of the provided links work for you, here's something of mine, though it will not be in the context of shift operations.
what's the maximum value to assign for a 64 - bit unsigned variable?
Some simple yet important concepts to remember
zero initialization, type conversions,type inference, constants, numeric constants
An untyped constant takes the type needed by its context.
For e.g., observe the following program:
package main
import "fmt"
const (
?? ?// Create a huge number by shifting a 1 bit left 100 places.
?? ?// In other words, the binary number that is 1 followed by 100 zeroes.
?? ?Big = 1 << 100
?? ?// Shift it right again 99 places, so we end up with 1<<1, or 2.
?? ?Small = Big >> 99
)
func needInt(x int) int { return x*10 + 1 }
func needFloat(x float64) float64 {
?? ?return x * 0.1
}
func main() {
?? ?fmt.Println(needInt(Small))
?? ?fmt.Println(needFloat(Small))
?? ?fmt.Println(needFloat(Big))
}
Untyped means a constant or a variable not declared with a "data type" explicitly but rather with arbitrary values implicitly. So, from the above example, the value "Small" wasn't float, but once it enters the needFloat it becomes a float value to support the operations performed within.