Strings, Runes, and Unicode

This time I'd like to cover some things fairly quickly. These should get me comfortable with the basics of GO. As I mentioned last time, I saw a lot of similarities between GO and Java (and similar languages). I'm hoping this will get me up and running quickly. If you're not familiar with these languages, you may want to dig deeper into these topics on your own. Again, these aren't really meant to be a tutorial as much as my thoughts and learning process. Let's dive in.

Last time I installed GO and got it to run a simple hello world program. Here it is again as a quick review and jumping-off point.

Screen Shot 2021-01-04 at 8.37.06 AM


Last time I'd wondered why the Println was capitalized, and everything else was lowercased. It turns out lowercase functions are private to the package, and uppercase ones are public. [Thanks @fribbledom]

One nice bit of syntax that GO has is the ability to import/include/source multiple packages in a single statement.


Screen Shot 2021-01-04 at 8.46.03 AM


This examples imports three packages. The thing I have to remember here is that the packages in the { } are not separated by commas. I'm sure I'll put commas between the package names through muscle-memory.

  • The three packages are:
  • fmt - Formatting output
  • strings - various string functions
  • math - things like minimums, maximums, etc.


Strings & Runes



String literals in GO are delimited by double-quotes ("), and escaped the standard way with a backslash ("\"). GO uses the term rune to refer to single characters. Cool! Runes are delimited by single-quotes (').

  • "Hello" is a string literal
  • "H" is a string literal
  • 'H' is a rune (single character)

GO also supports Unicode.
  • '' is a rune U+65E5 Unicode
  • "" is a string literal with the same Unicode

I dove right in and created a small program.



Screen Shot 2021-01-04 at 9.13.42 AM

[Yes, I know I'm breaking the rule of upper/lowercase public/private. I'm just playing at this point.]

I had to look up how to define a constant. Use the "const" keyword. Note, there is no type specification here.

It's not elegant. I just wanted to make it obvious. I create a string ("A") and print it. I create a rune ('A') and print it. I guessed the Println parameters would concatenate the way they do in many other languages.


Here's the output:


Screen Shot 2021-01-04 at 9.16.50 AM

Cool! It worked. Println prints runes as their ASCII/Unicode value; A = 65. So what happens with something Unicode? Here's my test.

Screen Shot 2021-01-04 at 9.23.39 AM

And the result comes out as expected.

Screen Shot 2021-01-04 at 9.23.52 AM

Summary



That's enough for today. At this point I have a good feel for:

  • Basic program structure
  • import
  • Println
  • Defining constants
  • Strings
  • Runes
  • Unicode
  • Running programs


This site does not track your information.