<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>x-vs-y on Golang</title><link>https://golang.k5kc.com/tags/x-vs-y/</link><description>Recent content in x-vs-y on Golang</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Sun, 16 Aug 2020 00:15:00 +0530</lastBuildDate><atom:link href="https://golang.k5kc.com/tags/x-vs-y/index.xml" rel="self" type="application/rss+xml"/><item><title>nil channel VS closed channel</title><link>https://golang.k5kc.com/2020/08/16/nil-channel-vs-closed-channel/</link><pubDate>Sun, 16 Aug 2020 00:15:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/nil-channel-vs-closed-channel/</guid><description>The zero value of channel type is nil, and the send and receive operations on a nil channel will always block. Check the following example:
package main import &amp;#34;fmt&amp;#34; func main() { var ch chan int go func(c chan int) { for v := range c { fmt.Println(v) } }(ch) ch &amp;lt;- 1 } The running result is like this:
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan send (nil chan)]: main.</description></item><item><title>"go build" and "go install"</title><link>https://golang.k5kc.com/2020/08/16/go-build-vs-go-install/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/go-build-vs-go-install/</guid><description>Let&amp;rsquo;s tidy up the $GOPATH directory and only keep Go source code files left over:
# tree . ├── bin ├── pkg └── src ├── greet │ └── greet.go └── hello └── hello.go 5 directories, 2 files The greet.go is greet package which just provides one function:
# cat src/greet/greet.go package greet import &amp;#34;fmt&amp;#34; func Greet() { fmt.Println(&amp;#34;नमस्ते किंशुक!&amp;#34;) } While hello.go is a main package which takes advantage of greet and can be built into an executable binary:</description></item><item><title>"nil slice" vs "nil map"</title><link>https://golang.k5kc.com/2020/08/16/nil-slice-vs-nil-map/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/nil-slice-vs-nil-map/</guid><description>Slice and map are all reference types in Go, and their default values are nil:
package main import &amp;#34;fmt&amp;#34; func main() { var ( s []int m map[int]bool ) if s == nil { fmt.Println(&amp;#34;The value of s is nil&amp;#34;) } if m == nil { fmt.Println(&amp;#34;The value of m is nil&amp;#34;) } } The result is like this：
The value of s is nil The value of m is nil When a slice&amp;rsquo;s value is nil, you could also do operations on it, such as append:</description></item><item><title>error vs errors</title><link>https://golang.k5kc.com/2020/08/16/error-vs-errors/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/error-vs-errors/</guid><description>Handling errors is a crucial part of writing robust programs. When scanning the Go packages, it is not rare to see APIs which have multiple return values with an error among them. For example:
func Open(name string) (*File, error)
Open opens the named file for reading. If successful, methods on the returned file can be used for reading; the associated file descriptor has mode O_RDONLY. If there is an error, it will be of type *PathError.</description></item><item><title>Pass slice as a function argument</title><link>https://golang.k5kc.com/2020/08/16/pass-slice-as-a-function-argument/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/pass-slice-as-a-function-argument/</guid><description>In Go, the function parameters are passed by value. With respect to use slice as a function argument, that means the function will get the copies of the slice: a pointer which points to the starting address of the underlying array, accompanied by the length and capacity of the slice. Oh boy! Since you know the address of the memory which is used to store the data, you can tweak the slice now.</description></item></channel></rss>