Skip to content

Instantly share code, notes, and snippets.

@MakotoE
Last active January 26, 2022 22:40
Show Gist options
  • Save MakotoE/8d4b09d4cd9d9279ae758e974a1bccb6 to your computer and use it in GitHub Desktop.
Save MakotoE/8d4b09d4cd9d9279ae758e974a1bccb6 to your computer and use it in GitHub Desktop.

I'm will be demonstrating in code, some of the things you can do with slices and maps in Go.

To run this program, I will use the command go run, and then dot, which represents the current directory. Running a Go program is that simple. What if I want to compile it? go build dot.

And what I like about this, compare to other compiled languages like C and Java, is that it's much easier to compile. No need to configure your includes, or your dynamic libraries. It almost feels like an interpreted language like Python.

In the code, this is the main function where I first call sliceExample1. In the first example, I allocate a slice of length 3. It prints it using the print line function. As you can see, the numbers1 slice has three items, all zero-initialized. I can get the length of the slice like this.

You can append an item to a slice by calling append with the slice in the first argument and the item in the second argument, then reassigning it to the slice. After this, you can see that 1 has been added to the slice.

In this second example, I initialized a slice, this time with numbers 0 to 8. 0 to 8. There are two ways to iterate through a slice. The first is the for loop with a counter. I'm printing i and the item at index i. As you can see, the indices and items are printed. Next is the for each loop. The range keyword returns a pair of an index and an item. This loop does the exact same thing as the other one. The only difference is that I don't have to access numbers2 from here; I get the item directly from the for loop.

This example shows how to get subslices. This is how you would get a single item from a slice. And you can get a subslice like this, which gives you a partial view of the entire slice. Here, 1 is the start index and 4 is the end index, which is exclusive. As you can see, it prints 1, 2, 3, stopping before 4. If you know Python, then you're probably familiar with this.

In the map example, I create countryCapitalMap, which is defined as a map of a string key and string value. Printing this outputs all of the entries which is quite useful in debugging. Let's say I wanted to add an entry; let's associate United States with Washington, D.C. And then I will print it, but this time using a for each loop. Again, range returns the key and value for each entry.

Capital of France is Paris, et cetera.

I can also delete an entry from the map using the delete keyword.

To check if France is still on the map, I can use this to get the value associated with key France and a boolean which signifies if the value exists. If the key is not in the map, then value is assigned the zero-value, which in the case of strings is the empty string. And as you can see, exists is false because France is no longer on our map.

That's all the time I have for my demo. Up next is Colin with a comparison between Go and JavaScript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment