Search This Blog

Sunday 1 November 2015

Literals in Go

BACKGROUND

  • Go source code is written in Unicode characters, encoded in UTF-8
  • Literals represent fixed, ie constant, values
  • There are two types of literals in Go, related to textual context: rune literals and string literals.

RUNE literals

Rune is an integer (uint32, 4 byte binary number), representing a Unicode code point, a unique identifier of a character within a particular encoding. In UTF-8, the most common Unicode encoding, a code point can represent a sequence of one to 4 bytes. ASCII (representing the old English and a group of unprintable characters) has 128 code points; extended ASCII, representing most Western languages 256.

Rune literal is expressed as one or more characters in single quotes, excluding unquoted single quotes and newlines.

STRING literals

String literal is a concatenation of characters, a character sequence. There are two types: interpreted string literals and raw string literals.
Interpreted string literals are enclosed in double quotes with any character allowed, except for unquoted double quote and newline.

Raw string literals are enclosed in back quotes. The character sequence can contain newlines and backslashes have no special escaping effect. Any carriage returns (\r) within the literal are stripped from the raw string.

Example


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
package main

import (
 "log"
 "net/http"
)

func main() {

 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  w.Write([]byte('<html><head>
    <title>Chatting away</title>
    </head><body>Go and chat!</body></html>
    '))
 })

 if err := http.ListenAndServe(":3333", nil); err != nil {
  log.Fatal("Starting server : ", err)
 }

}

The above code produces errors on lines 11 and 14: rune literal not terminated.

Keeping the single quotes and putting the whole string value on one line results in:  illegal rune literal. Single quotes are reserved for characters, so no surprise there.

Changing the single quotes to double quotes still results in error, this time: string literal not terminated. You may find it puzzling, just as I did, until you realize this is an interpreted string literal (because enclosed in double quotes) and recall these cannot contain newlines.

There are two options here:
  1. Put the whole string value on one line:
            "<html><head><title>Chatting away</title></head><body>Go and chat!</body></html>"
  1. Use back quotes, ie use a raw string literal, rather than an interpreted one. This will allow to spread the string value over several lines, because the back quoted raw string literal removes the carriage return: 
             `<html>
              <head><title>Chatting away</title></head>
              <body>Go and chat!</body>
              </html>`