Iman Tung

We are constantly in a work-and-learn situation. The more we learn, the easier we should be at work. With continued work, we can’t stop learning.

How good are we? TBH it is not important at all.

Good or bad, we keep working and learning (there is no escape). Our competency is built by continuous efforts from both.

--

--

Today I found out that json.Marshal() weirdly return null for an empty slice but make sense.

var slice []string
b, _ := json.Marshal(slice)
fmt.Println(string(b)) // Output: null

The workaround is to initiate empty slice using make() function.

var slice []string
slice = make([]string, 0)
b, _ := json.Marshal(slice)
fmt.Println(string(b)) // Output: []

I thought make() will make fixed length slice and append() will trigger an error, but it totally fine. That's why in go they called it to slice instead of an array, the behavior is slightly different with the fixed-length array in another language.

var slice []string
slice = make([]string, 0)
fmt.Println(len(slice)) // Output: 0

slice = append(slice, "some-string")
fmt.Println(len(slice)) // Output: 1

b, _ := json.Marshal(slice)
fmt.Println(string(b)) // Output: ["some-string"]

Thanks to Dan Ott’s article for the insight

Previously published in https://imantung.github.io at 18 Apr 2019

--

--

“If you don’t know how compilers work, then you don’t know how computers work. If you’re not 100% sure whether you know how compilers work, then you don’t know how they work.” — Steve Yegge

Like a few weeks ago I did research about interpreters for a DSL-related project.

This approach called Tree-Walking Interpreter is the simplest way to make an interpreter

  1. Free text/grammar: literally what you type
  2. Lexer/lexical analysis: text to tokens
  3. Tokens/Lexemes: chunks of your syntax
  4. Parser: tokens to the syntax tree
  5. Syntax Tree/Abstract Syntax Tree or AST: model represented operator with its precedence level and associativity
  6. Evaluator: running through AST and execute the operation.

For those who looking for more resources to start learning on this subject, I recommend Ruslan Blog and Writing An Interpreter In Go

Previously published in https://imantung.github.io at 20 Dec 2017

--

--

Iman Tung

Iman Tung

Technology to write, life to grateful. Overthinking is good, only if it has the output. Fundamental is the main concern.