Today's Question:  What does your personal desk look like?        GIVE A SHOUT

SEARCH KEYWORD -- #nil



  Be careful about nil check on interface in GoLang

nil check is frequently seen in GoLang code especially for error check since GoLang's special error handling convention. In most cases, nil check is straight forward, but in interface case, it's a bit different and special care needs to be taken. Take a look at below code snippet and guess what the output will be. package main import ( "bytes" "fmt" "io" ) func check(w io.Writer) { if w != nil { fmt.Println("w is not nil") } fmt.Printf("w is %+v\n", w) } func main() { var b *bytes.B...

   INTERFACE,GOLANG,NIL CHECK,NIL TYPE,NIL VALUE     2019-04-06 07:47:07

  Empty slice vs nil slice in GoLang

In Go, there is a type called slice which is built on top of array. It's a very convenient type when we want to handle a group of data. This post will explain a subtle but tricky difference between empty slice and nil slice. A nil slice is a slice has a length and capacity of zero and has no underlying array. The zero value of slice is nil. If a slice is declared like below, it is a nil slice. package main import "fmt" func main() { var a []string fmt.Println(a == nil) } The output will be t...

   GOLANG,JSON,EMPTY SLICE,NIL SLICE     2018-10-18 09:25:21

  Will nil == nil be true in GoLang

There is some interview question may ask whether nil == nil be true in GoLang. Wil it be true. false or compilation error? To know the answer, some knowledge about nil in GoLang needs to be explained first. nil definition According to Go's official documentation, the definition of nil is // nil is a predeclared identifier representing the zero value for a // pointer, channel, func, interface, map, or slice type. var nil Type // Type must be a pointer, channel, func, interface, map, or slice type...

   GOLANG,NIL,NIL EXPLANATION     2021-08-08 02:54:15

  How to Understand and Use nil in Golang Correctly?

In Golang, nil is a predefined identifier that carries different meanings in various contexts, but typically represents "none", "empty" or "zero value". It can be assigned to variables of pointer, slice, map, channel, function, and interface types. Understanding the significance of nil is crucial for writing robust Go programs, as mishandling nil can lead to unexpected issues. nil in Pointers In Go, pointers are a fundamental type that stores the memory address of a variable. When a pointer is d...

   FUNCTION,SLICE,MAP,CHANNEL,GOLANG,NIL     2024-01-05 05:19:40

  Implementing DESede/ECB/NoPadding cipher algorithm in GoLang

By default, GoLang doesn't provide the ECB mode cipher for DESede though there is CBC mode provided. In cases we need to encrypt/decrypt data with ECB mode, we need to implement those by ourselves. This mode is frequently used when encrypting/decrypting PIN block which is small block data less than 16 bytes. In this post, we will introduce how to implement the DESede/ECB/NoPadding algorithm in GoLang by using the existing cipher support. Here we will not cover how DESede works in detail, instead...

   SECURITY,SAMPLE,GOLANG,DES,DESEDE,3DES     2019-07-29 06:43:50

  Use downcase! with caution in Ruby

Ruby provides ! to change state of some object in place. Hence if you see some functions have ! appended, it means the state of the caller of the function is expected to be changed. This is a very interesting Ruby feature. But sometimes one should be cautious when using this kind of functions because you would get unexpected behavior if using improperly. Let's take an example of String#downcase!. According to the documentation. Downcases the contents of str, returning nil if...

   RUBY,EXCLAMATION MARK,DOWNCASE     2017-02-10 06:34:44

  Singleton Pattern in Golang

Singleton pattern is the simplest design pattern in software design. It ensures that only one instance of an object exists globally, regardless of how many times the object is instantiated. Based on the characteristics of the singleton pattern, it can be applied to scenarios such as global unique configuration, database connection objects, file access objects, etc. In Go language, there are multiple ways to implement the singleton pattern. Today, let's learn together about some of these approach...

   GOLANG,SINGLETON PATTERN,TUTORIAL     2023-08-18 23:52:05

  errGroup in GoLang explained

Today, let's talk about the usage and applicable scenarios of errGroup, which is often used. Why is it useful? Generally, when we use goroutine, we cannot return a value. If you want to pass out the result of the goroutine execution, you usually have to use the channel. The errGroup package is suitable if you want to know when the goroutine you opened encounters an error during execution and stop working, and I need to know the error value. errGroup usage The package needs to be downloaded and i...

   GOLANG,ERRGROUP,WAITHROUP,CONCURRENCY     2023-05-27 23:58:20

  Guide to Implement an SSH Client Using Golang

SSH, short for Secure Shell, is a network protocol used for securely remote logging into other computers on a network. I believe most backend developers are familiar with SSH. Common shell tools used for logging into servers, such as Xshell, SecureCRT, and iTerm2, are all based on the SSH protocol. In Golang, the crypto/ssh package provides functionality for implementing an SSH client. In this article, we will explain in detail how to implement an SSH client using Golang. Creating SSH Client Con...

   SSH CLIENT,GUIDE,SSH,GOLANG     2023-11-11 09:19:29

  Error handling in GoLang

Error handling is one of the must talked topics for any programming language. The program would be more reliable and stable if errors are handled properly and timely. Each programming language has its own way to handle error, this applies to GoLang as well. This post will discuss more about GoLang's error handling mechanism. Error handling Before talking more about GoLang's error handling, we can see how different programming languages are handling errors. C's error check The most direct way of ...

   GOLANG,ERROR HANDLING,FLUENT INTERFACE     2021-03-06 21:36:08