<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>go-types on Golang</title><link>https://golang.k5kc.com/categories/go-types/</link><description>Recent content in go-types on Golang</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Sun, 16 Aug 2020 00:10:00 +0530</lastBuildDate><atom:link href="https://golang.k5kc.com/categories/go-types/index.xml" rel="self" type="application/rss+xml"/><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 - Mutability</title><link>https://golang.k5kc.com/2020/08/16/mutability/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/mutability/</guid><description>In Go, only constants are immutable. However because arguments are passed by value, a function receiving a value argument and mutating it, won’t mutate the original value.
package main import &amp;#34;fmt&amp;#34; type Artist struct { Name, Genre string Songs int } func newRelease(a Artist) int { a.Songs++ return a.Songs } func main() { me := Artist{Name: &amp;#34;Matt&amp;#34;, Genre: &amp;#34;Electro&amp;#34;, Songs: 42} fmt.Printf(&amp;#34;%s released their %dth song\n&amp;#34;, me.Name, newRelease(me)) fmt.Printf(&amp;#34;%s has a total of %d songs&amp;#34;, me.</description></item><item><title>Go - Named and Unnamed Types</title><link>https://golang.k5kc.com/2020/08/16/named-vs-unnamed-types/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/named-vs-unnamed-types/</guid><description>Types in Go are divided into 2 categories: named and unnamed. Besides predeclared types (such as int, rune, etc), you can also define named type yourself. E.g.:
type mySlice []int Unnamed types are defined by type literal. I.e., []int is an unnamed type.
According to Go spec, there is an underlying type of every type:
Each type T has an underlying type: If T is one of the predeclared boolean, numeric, or string types, or a type literal, the corresponding underlying type is T itself.</description></item><item><title>Go - String</title><link>https://golang.k5kc.com/2020/08/16/3.string/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/3.string/</guid><description>In Go, string is an immutable array of bytes. So if created, we can&amp;rsquo;t change its value. E.g.:
package main func main() { s := &amp;#34;Hello&amp;#34; s[0] = &amp;#39;h&amp;#39; } The compiler will complain:
cannot assign to s[0]
To modify the content of a string, you could convert it to a byte array. But in fact, you do not operate on the original string, just a copy:
package main import &amp;#34;fmt&amp;#34; func main() { s := &amp;#34;Hello&amp;#34; b := []byte(s) b[0] = &amp;#39;h&amp;#39; fmt.</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 - Type conversion</title><link>https://golang.k5kc.com/2020/08/16/4.type-conversion/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/4.type-conversion/</guid><description>The expression T(v) converts the value v to the type T. Some numeric conversions:
var i int = 42 var f float64 = float64(i) var u uint = uint(f) Or, put more simply:
i := 42 f := float64(i) u := uint(f) Go assignment between items of different type requires an explicit conversion which means that you manually need to convert types if you are passing a variable to a function expecting another 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>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>