cancel
Showing results for 
Search instead for 
Did you mean: 
Login & Join the DevCentral Connects Group to watch the Recorded LiveStream (May 12) on Basic iControl Security - show notes included.

Related Articles

Go in Plain English: Creating your First WebServer

Quick Intro

In this article I'm going to start with Strings to give you some hands-on exposure.

You need to install Go first and ideally a code editor like VS Code as shown in Golang in Plain English: Settings things up and Hello World! 

The way I'll go through variables here is the way I taught myself Go so it shouldn't be hard.

Just repeat what I do here and you should be fine. Here's what I'll go through:

  • Declaring a variable in Go (you'll even understand the mysterious :=) 
  • Using just parts of Strings with Slicing
  • Iterating through String Characters
  • Printing the Length of a String
  • How do we print double quotes inside of print statement?

Declaring a variable in Go

This is how we declare a variable in Go:

0151T000003kknuQAA.png

Go makes it easier by letting you both declare and set the variable at the same time by adding a colon before the equals sign like this:

0151T000003kknvQAA.png

However, if I try to run it Go won't let me because it requires that all declared variables must be used:

0151T000003kknzQAA.png

So if we add a print statement it will work:

0151T000003kko4QAA.png

Note that %s means we're printing a string. If it was an integer, for example, you'd use %d instead.

It now works:

0151T000003kko5QAA.png

Using just parts of the string with Slicing

Let's say you just want to print DevCentral (character 0 to 10) but you don't want to touch the variable.

This is what you do:

0151T000003kko9QAA.png

Here's the output:

0151T000003kkoAQAQ.png

You could've omitted the 0 leaving just my variable[:10] and Go would understand that starting position is 0 anyway.

We can also do the other way round if we want to print just awesome (character 14 until the end).

This time I'm just going to omit the last position as Go understands it's up to the end:

0151T000003kko6QAA.png

Here's the output:

0151T000003kkoEQAQ.png

Iterating through string characters

The other MUST know is how to go through each character in a for loop using range:

0151T000003kkoJQAQ.png

The first thing to understand here is that our for loop requires 2 things: the index we're at (0,1,2,3, etc) and the character.

So first iteration: i = 0, c = 'D', 2nd iteration: i = 1, c = 'e', and so on.

Let's print this out:

0151T000003kkoOQAQ.png

Let's say you don't care about the index.

In that case, Go has something very handy for this.

Just use the _ character and Go will ignore it:

0151T000003kkoTQAQ.png

Here we go:

0151T000003kkoPQAQ.png

If it's just the index, you don't need the _ because Go already understands that you just want to print the index as it's the first entry (from i,c):

0151T000003kkoYQAQ.png

And here's the output again:

0151T000003kkoQQAQ.png

Printing the Length of a String

Another useful thing is to learn how to print the length of a string which is not very different from other programming languages:

0151T000003kkoUQAQ.png

Here's the output:

0151T000003kkoZQAQ.png

How do we print double quotes inside of print statement?

Just put your string between back quotes like this:

0151T000003kkodQAA.png

Let's run it:

0151T000003kkoaQAA.png

Oops! It also printed our new line character. Let's fix it by taking it out of our back quotes and merging it between double quotes like this:

0151T000003kkoVQAQ.png

Let's run it one more time:

0151T000003kkoBQAQ.png

Fixed!

Version history
Last update:
‎19-Aug-2019 03:37
Updated by:
Contributors