Pointer or not and mutable or not If you don’t know what’s a pointer or want a reminder you may read the excelent Dave Cheney post : Understand Go pointers in less than 800 words or your money back When should I use pointer, when should I use reference ? TLDR: Methods using receiver pointers are common; the rule of thumb for receivers is, “If in doubt, use a pointer.

Continue reading

Golang and Unicode First what’s a string in go ? In go a string is an immutable slice of byte. In go every string you declare is UTF-8 encoded (but the one you get from user may have different encoding; you must absolutely know the encoding of a string you get). Some definition Unicode define every letter in the world as code points. UTF-8 is a manner to encode all this code point (there is many : UTF-16, UTF-32, UTF-7), UTF-8 is the one used for best space efficiency : a char can be 1 byte (with ASCII table compatibility) or more (up to 4); comparing to UTF-16 where the minimum size is 2 bytes; UTF-32 is best for speed in string manipulation (knowing that every code points is always 4 bytes enable random access : string slicing is O(1) in UTF-32 and O(n) in UTF-8) … and UTF-7 is a bizarre Unicode unofficial encoding used only by imap, let’s not worry about it.

Continue reading

SQL scan in Go without headache If this is first time you do sql with Go you probably should read this tutorial first, and comme back here to improve scan. TLDR: Use sqlx for doing SQL. sqlx sit on top of standard sql library adding very convenient function especialy to scan result to a struct. SQL scan without sqlx Sql scan is a little bit tedious in Golang : the scan order must match your SQL query order.

Continue reading

or the unexpected behaviour of nullable type from sql to JSON Problem description When working with a database you will have to deal with nullable type, you have sql.null type in standard library. But if you use JSON, you will find that they don’t encode/decode in JSON has we intend : they expose their internal; example : a SqlnullInt (valid, and value 42) encoded in JSON will look like : { "number": { "Valid": true, "Int64": 42 } } but you wanted : { "number": 42 } Simple solution to encode to JSON Behaviour of the Go standart library is easy to overload : simply redefine the MarshalJSON for each type you want Here is an example to export a SQL NullInt64 as expected in JSON :

Continue reading

Some ressources The first 2 are sufficient to be ready to code with Go in 1 day ! Official go site : How to write Go code Golang bot : Golang tutorial series Go by example A tour of Go 50 shades of Go There is some other : to fully understand slice, map, context etc there is the official Go blog, and I also like to read Dave Cheney

Continue reading

Author's picture

Tristan Giovangrandi

Happy Web developper @Content-Square and happy father

Full Stack & Embedded

France - Paris