Golang Aesthetic Notes #2
? Kagemicrotank, Shutterstock

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:

Reference.

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:

Reference

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:

Stackoverflow, IBM

  • Range of absolute values i.e., signed integers range over only half as many absolute integer values as unsigned integers do but they can represent both signs for those values.
  • So, by extension unsigned integers can't represent the negative values but they can represent twice as many positive values as signed integers can.
  • Here's a wiki page on using two's complement to find the negative equivalent of a binary number.

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?

  • So, firstly what does it mean to say a 64 - bit value? It means the value has 64 "bits", where a bit can be either a 1 or a 0. Okay, that was probably too obvious.
  • It would probably be easy for you to imagine that the biggest 64 bit value would look like a number made up of just 64 1's. i.e., 111111.. ( 64 times ).
  • Now the decimal integer that this 64 bit value will translate to is the highest value that can be assigned to a variable with uint64 data type.
  • To calculate that we need sum of n terms in a geometric sequence. So, the formula is a1 * (1 - r^n) / ( 1 - r ) . We want to calculate sum of powers of 2 till 63 since the count start from zero. Then, the expression would come down to, guess what: 2^64 - 1 and this is what 1<<64 - 1 means.
  • So setting 1 << 64 will make Go yell back with an error message.

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.

要查看或添加评论,请登录

Vishak Arudhra的更多文章

  • Understanding CNI Part 2 (Container Network Interface)

    Understanding CNI Part 2 (Container Network Interface)

    This time, we pick up from where the last article concludes in Part 1 which should be enough to understand, on a…

  • Service Discovery With Consul

    Service Discovery With Consul

    There are several challenges faced with a traditional load balancer setup and Service Discovery and Mesh can solve them…

  • Understanding CNI (Container Network Interface) - laying out the essential bits

    Understanding CNI (Container Network Interface) - laying out the essential bits

    Is CNI, a piece of software or just a set of standard rules? It's both. But to some, it may not be very straight…

  • Euclid to RSA - Asymmetric Encryption

    Euclid to RSA - Asymmetric Encryption

    At the foundation of RSA are discoveries of popular mathematicians whose formulas shall be discussed with practical…

    2 条评论
  • Golang Aesthetic Notes #7

    Golang Aesthetic Notes #7

    How Methods differ from Functions Methods defined for pointer receivers also work for 'value' type variables Say…

  • Golang Aesthetic Notes #6

    Golang Aesthetic Notes #6

    Passing Functions as Values Concept of functions as values is really as simple as passing values into function. For e.

  • Golang Aesthetic Notes #5

    Golang Aesthetic Notes #5

    When a variable already representing a memory location is assigned with a new value what happens to that memory…

  • Golang Aesthetic Notes #4

    Golang Aesthetic Notes #4

    Deferring (and Loops) Exploring the part of the tutorial on 'Deferring'. I also got to understand a little better about…

    3 条评论
  • Golang Aesthetic Notes #3

    Golang Aesthetic Notes #3

    while loop is just 'hacked' for - loop Reference For e.g.

  • Golang Aesthetic Notes #1

    Golang Aesthetic Notes #1

    Golang for the beginner. So I just started learning.

社区洞察

其他会员也浏览了