When would you use single quotes?

159 views
Skip to first unread message

mr.jamie...@gmail.com

unread,
Feb 6, 2019, 6:14:41 PM2/6/19
to golang-nuts
Hello,

I'd be grateful if someone could please explain why you would use

r := '⌘'

Instead of 

s := "⌘" / s:= `⌘`

All use three bytes ...?

Thank you,
Jamie.

Wagner Riffel

unread,
Feb 6, 2019, 6:30:46 PM2/6/19
to mr.jamie...@gmail.com, golang-nuts
'⌘' is of type rune (aka int32), "⌘" and `⌘` are of type string, both
takes more than 3 bytes.

Jamie Caldwell

unread,
Feb 7, 2019, 5:25:39 AM2/7/19
to Wagner Riffel, golang-nuts
Thank you for getting back to me, but I don't think you have answered my question.

I understand they are a rune and string respectively.  But why would you use one over the other?  Why does Go support being able to assign a codepoint using single quotes?

Also, why do they take more than three bytes each?

Thank you.

Jan Mercl

unread,
Feb 7, 2019, 5:50:41 AM2/7/19
to Jamie Caldwell, Wagner Riffel, golang-nuts
On Thu, Feb 7, 2019 at 11:25 AM Jamie Caldwell <mr.jamie...@gmail.com> wrote:

> But why would you use one over the other? Why does Go support being able to assign a codepoint using single quotes?

`type rune` vs type `string` not the same, but is bit like `type byte` vs `type []byte`. The serve very different purposes. One cannot do the same things with `byte` that can be done with `[]byte`.


> Also, why do they take more than three bytes each?

`rune` is an alias of `int32`, hence 4 bytes.

`string` is a two word struct, hence 2*size of pointer. 8 or 16 bytes.

--

-j

Tamás Gulácsi

unread,
Feb 7, 2019, 5:51:44 AM2/7/19
to golang-nuts
A rune is an int32, so it takes 4 bytes by definition.
A string in a struct with position, length and backing array of bytes. The backing array here consumes 3 bytes, but tge position and length occupies space too, so the string of that rune occupies more than 3 bytes after all.

Jamie Caldwell

unread,
Feb 7, 2019, 6:48:01 AM2/7/19
to Tamás Gulácsi, golan...@googlegroups.com
Thank you both for your answers.  It is much appreciated.

The UTF8 encoding of that codepoint is three bytes.  So the rune will still occupy 4 bytes, even if the last byte holds no data? I'm sorry for the school boy question!

Thank you.

On Thu, 7 Feb 2019, 10:52 Tamás Gulácsi <tgula...@gmail.com wrote:
A rune is an int32, so it takes 4 bytes by definition.
A string in a struct with position, length and backing array of bytes. The backing array here consumes 3 bytes, but tge position and length occupies space too, so the string of that rune occupies more than 3 bytes after all.

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/-bvJLkhX_dY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

On Thu, 7 Feb 2019, 10:52 Tamás Gulácsi <tgula...@gmail.com wrote:
A rune is an int32, so it takes 4 bytes by definition.
A string in a struct with position, length and backing array of bytes. The backing array here consumes 3 bytes, but tge position and length occupies space too, so the string of that rune occupies more than 3 bytes after all.

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/-bvJLkhX_dY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Volker Dobler

unread,
Feb 7, 2019, 7:22:42 AM2/7/19
to golang-nuts
The UTF8 encoding of that codepoint is three bytes.  So the rune will still occupy 4 bytes, even if the last byte holds no data? 

A rune has nothing to do with UTF-8.
A rune stores the codepoint which is totally independent
of any encoding (like UTF-8, UTF-16, UTF-23, EBCDIC, whatnot).

A rune is an integer, the number of the codepoint.
An integer is stored in a certain number of bytes.
Asking  "So the rune will still occupy 4 bytes, even if the last
byte holds no data?" is like asking "So the number 12 will
still occupy 8 bits, even if the last 5 bits hold no data?".
Yes. An integer is stored in 8 bytes (64bit architecture)
and this is true even for "small" integers which would "fit"
into one byte.

A rune is an integer. It has nothing to do with UTF.

V.

peterGo

unread,
Feb 7, 2019, 8:16:06 AM2/7/19
to golang-nuts
Jamie,

This is a question about Unicode:

The Unicode Consortium: http://unicode.org/

The Unicode Standard: http://www.unicode.org/standard/standard.html

Unicode Frequently Asked Questions: UTF-8, UTF-16, UTF-32 & BOM: http://www.unicode.org/faq/utf_bom.html

Briefly, a Unicode code point is 24 bits. The nearest common hardware equivalent is 32 bits. Go uses type int32. Go uses an alias of type rune to distinguish code points from integers.

A Unicode transformation format (UTF) is an algorithmic mapping from every Unicode code point to a unique byte sequence. Go favors UTF-8.

In Go, single quotes enclose a rune (32 bit) literal, double quotes enclose a UTF-8 encoded string (one to four byte) literal.

Peter

Jamie Caldwell

unread,
Feb 8, 2019, 5:15:05 AM2/8/19
to golang-nuts
Volker / Jan / Tamás & Peter -- thank you all for your replies.

--
Reply all
Reply to author
Forward
0 new messages