<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>switch on Golang</title><link>https://golang.k5kc.com/tags/switch/</link><description>Recent content in switch on Golang</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Sun, 16 Aug 2020 00:13:00 +0530</lastBuildDate><atom:link href="https://golang.k5kc.com/tags/switch/index.xml" rel="self" type="application/rss+xml"/><item><title>Go - Maps</title><link>https://golang.k5kc.com/2020/08/16/4.maps/</link><pubDate>Sun, 16 Aug 2020 00:13:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/4.maps/</guid><description>Intro Maps are somewhat similar to what other languages call “dictionaries” or “hashes”. A map maps keys to values.
Here are some points to look at:
Maps allows us to quickly access to an element/value using a unique key. Map keys must be unique because otherwise it can&amp;rsquo;t find the corresponding values/elements. The types of Map Keys and Values in Maps can be different. A Map Key must be a comparable type.</description></item><item><title>Attaching Method to any type</title><link>https://golang.k5kc.com/2020/08/16/attaching-method-to-any-type/</link><pubDate>Sun, 16 Aug 2020 00:12:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/attaching-method-to-any-type/</guid><description> We can attach methods to any type in Go. For e.g.
// Basic Types int string float64 // Bare Types array struct // ----------------------- // Do not use &amp;#34;Pointer Receivers&amp;#34; with below types since they already carry a pointer with themselves. // i.e. slice, map, chan, func // ----------------------- // Pointer Bearing Types slice map chan // Channels // We can also attach methods to: func</description></item><item><title>Go Conditionals - switch</title><link>https://golang.k5kc.com/2020/08/16/2.switch/</link><pubDate>Sun, 16 Aug 2020 00:11:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/2.switch/</guid><description>Intro In addition to the switch keyword, a switch statement has cases. The switch statement switches on some case.
Example - No break required Compared to other programming languages (such as C), Go&amp;rsquo;s switch-case statement doesn&amp;rsquo;t need explicit &amp;ldquo;break&amp;rdquo;, and not have fall-though characteristic. Take the following code as an example:
package main import ( &amp;#34;fmt&amp;#34; ) func checkSwitch(val int) { switch val { case 0: case 1: fmt.Println(&amp;#34;The value is: &amp;#34;, val) } } func main() { checkSwitch(0) checkSwitch(1) } The output is:</description></item><item><title>Error handling intro</title><link>https://golang.k5kc.com/2020/08/16/1.error-handling/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/1.error-handling/</guid><description>nil is a predeclared identifier like true, false, len(), int32, float64 etc.
Since it is a predeclared identifier, it can be used anywhere without importing any package.
nil value means that the value is not initialized yet.
It is similar to following identifiers in other languages:
null // JavaScript None // Python null // Java nil // Ruby The zero value of all pointer-based types in Go is nil. Following are the pointer-based types in Go:</description></item><item><title>Error handling intro</title><link>https://golang.k5kc.com/2020/08/16/2.error-interface/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/2.error-interface/</guid><description>Error is by far the most used interface in Go. lets have a look at it:
type error interface { Error() string } //The error built-in interface type is the conventional interface for representing an error condition, with the nil value representing no error. So any type that implements the Error method is by default an error type. Some Examples:
package main import &amp;#34;fmt&amp;#34; type MyError string func (me MyError) Error() string{ return string(me) //cast it back to string } type MyOtherError struct{ Code int Message string } func (me MyOtherError) Error() string{ return fmt.</description></item><item><title>Go - Basic Data Type</title><link>https://golang.k5kc.com/2020/08/16/2.basic-data-types/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/2.basic-data-types/</guid><description>Following are the basic data types in Go:
Numeric // Integer Types uint8 // Unsigned 8-bit integers (0 to 255) uint16 // Unsigned 16-bit integers (0 to 65535) uint32 // Unsigned 32-bit integers (0 to 4294967295) uint64 // Unsigned 64-bit integers (0 to 18446744073709551615) int8 // Signed 8-bit integers (-128 to 127) int16 // Signed 16-bit integers (-32768 to 32767) int32 // Signed 32-bit integers (-2147483648 to 2147483647) int64 // Signed 64-bit integers (-9223372036854775808 to 9223372036854775807) // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ // Floating Types float32 // IEEE-754 32-bit floating-point numbers float64 // IEEE-754 64-bit floating-point numbers complex64 // Complex numbers with float32 real and imaginary parts complex128 // Complex numbers with float64 real and imaginary parts // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ // Other Numeric Types byte // same as uint8 rune // same as int32 uint // 32 or 64 bits int // same size as uint uintptr // an unsigned integer to store the uninterpreted bits of a pointer value Handling Overflows At compile time, a Go compiler can catch overflow errors.</description></item><item><title>Go - Data Type Intro</title><link>https://golang.k5kc.com/2020/08/16/1.data-types-intro/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/1.data-types-intro/</guid><description>There are following data types in Go:
Basic type: Numbers, strings, and booleans come under this category. Aggregate type: Array and structs come under this category. Reference type: Pointers, slices, maps, functions, and channels come under this * category. Interface type</description></item><item><title>Go - Structs</title><link>https://golang.k5kc.com/2020/08/16/1.struct/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/1.struct/</guid><description>Intro A struct is a collection of fields/properties. You can define new types as structs or interfaces. If you are coming from an object-oriented background, you can think of a struct to be a light class that supports composition but not inheritance.
Structs are blueprints — They are fixed and created at compile-time, but struct values fill them in runtime..
It&amp;rsquo;s like a class in OOP languages. Groups related data in a single type.</description></item><item><title>Go Pointers</title><link>https://golang.k5kc.com/2020/08/16/pointers/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/pointers/</guid><description>Go has pointers, but no pointer arithmetic. Struct fields can be accessed through a struct pointer. The indirection through the pointer is transparent (you can directly call fields and methods on a pointer).
Note that by default Go passes arguments by value (copying the arguments), if you want to pass the arguments by reference, you need to pass pointers (or use a structure using reference values like slices (Section 4.2) and maps (Section 4.</description></item><item><title>Method with struct and interface</title><link>https://golang.k5kc.com/2020/08/16/methods-with-struct-and-interface/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/methods-with-struct-and-interface/</guid><description>Methods enhance types with additional behavior.
Methods of the type are called Method Set.
To attach method to a type :
// Syntax // &amp;ldquo;varName Type&amp;rdquo; is called a &amp;ldquo;receiver&amp;rdquo; func (varName Type) funcName() { // Code }
// Example // &amp;ldquo;book&amp;rdquo; is a struct here func (b book) printBook() { fmt.Println(b.title, b.price) }
- A `receiver` is nothing but method&amp;#39;s `input parameters` written `before` a `method name`. - A `method` belongs to a `single type`.</description></item><item><title>Nil values</title><link>https://golang.k5kc.com/2020/08/16/nil-values/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/nil-values/</guid><description/></item><item><title>Printf and Sprintf formatting</title><link>https://golang.k5kc.com/2020/08/16/printf-sprintf-formatting/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/printf-sprintf-formatting/</guid><description>Following formatting can be used with fmt.Printf as well as fmt.Sprintf:
// String and slice of bytes %s // the uninte­rpreted bytes of the string or slice %q // a double­-quoted string safely escaped with Go syntax %x // base 16, lower-­case, two characters per byte %X // base 16, upper-­case, two characters per byte // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ // Boolean %t // the word true or false // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ // General %v // The value in a default format.</description></item><item><title>Slice vs Arrays</title><link>https://golang.k5kc.com/2020/08/16/slice-vs-array/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/slice-vs-array/</guid><description>Performance Slice operations are cheap! Slicing: Creates a new slice header. Assigning a Slice to another Slice or, passing it to a function: Only copies the slice header. Slice header has a fixed size and it doesn't change even if we have got millions of elements. Array can be expensive as compared to Slice. Assigning an array to another array or passing it to a function: Copies all the elements of it.</description></item><item><title>Type assertion and type switch</title><link>https://golang.k5kc.com/2020/08/16/5.type-assertion-and-type-switch/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/5.type-assertion-and-type-switch/</guid><description>If we have a value and want to convert it to another or a specific type (in case of interface{}), we can use type assertion. A type assertion takes a value and tries to create another version in the specified explicit type.
This is how we can use type assertion:
x.(T) x is the variable whose type must be interface, and T is the type which you want to check. For example:</description></item><item><title>Zero Values</title><link>https://golang.k5kc.com/2020/08/16/zero-values/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/zero-values/</guid><description>When a variable is declared and it isn&amp;rsquo;t assigned any value at the time of declaration, Go will assign a zero value to it based on it&amp;rsquo;s variable type.
Type of a variable decides what a zero value to it will take initially when declared (and if it isn&amp;rsquo;t assigned any value at the time of declaration).
// Zero Values assigned to variables by Go when they are declared and not assigned any values at the time of declaration.</description></item></channel></rss>