From the course: Learning Go

Experiment with the Go Playground - Go Tutorial

From the course: Learning Go

Experiment with the Go Playground

- [Instructor] You can find nearly everything you need to get started with Go at golang.org. You'll find documentation, code packages, downloadable binaries, and the Go Playground. The Go Playground is an in browser editor. And there's a version of it embedded on this webpage. And from here, you can add and customize your Go code and run it instantly. The embedded version of the Go Playground on the homepage has a simple hello world application and you can immediately run it. Notice the message waiting for remote server. And then after the code is executed you'll see the result right here on the screen. You can make changes. So for example, I'm going to change this to my name and run the code again. There's also a full screen version of the GO Playground at playdotgolang.org. And once again, it starts with the hello world application. When you run it, you're doing exactly the same thing. This pull-down list offers various code templates. The tests application loops through a bunch of information and outputs the result, throwing an error when it gets past a particular index level. There's a multiple files application that actually places all the code into one single file, and when you run that, you'll see the result. And there are a few other examples as well. Now, if you go back to the homepage and pull down the list of available samples you'll see that the samples here are different. So for example, if I wanted to run the Fibonacci Closure application I could select it and run it. But if I want to use that in the full screen version of the Playground, I can click share. Then I see exactly the same code and I can run it from here. You can also fix formatting with the Go Playground. I'm going to take out some of these tab characters and make everything flushed to the left side. And then I'll click the format button and everything is returned back to the way it was. In the background the Go Playground is simply using the Go Command with formatting commands. And as you'll see, when we get into working with Go in visual studio code, you can do the formatting easily there as well. You can also save or share your own custom code. I'm going to go back to the hello playground application. And I change this to my name and click run. And now I can share that code with other folks by clicking the share button. I'll get an automatically generated URL and I can copy that and then I can send it to somebody and when they go to that URL they'll see my code instead of the template code that's available to everybody in the playground. It's important to understand that the Go Playground is using a backend server to compile your application and return the results. And that creates some important limitations compared to Go based applications you might run on your own computer. First, the Go Playground runs in a security sandbox and the applications running into Go Playground don't have access to the outside world. So you can't, for example, make requests to other external hosts on the web and you can't host your own web services. The local host address 127.0.0.1 still works for many examples, but you won't be able to get to host outside of that environment. The playground also fakes the file system. It simulates read and write operations so you can write the files that you create and then read them within the same application run, but the changes aren't persistent and there are limitations to what you can do. Also in the Go Playground it's always the same date in time, specifically November 10th, 2009 at 11:00 PM. If you run sample code in the playground that works with dates and times, you'll always see this information no matter what date and time it really is. Why this date and time? Because that's when Go was first announced. And that's fun, but beyond the inside joke, there's a functional purpose to this. By using the same date and time values all the time for an environment that's mostly used for learning Go it makes the results, what we call determinative. That means it's always the same. So you can compare your results to the results of any sample code you might be following along with. The Go Playground is completely free with no limitations. You don't have to register to use it and you don't have to pay any licensing fees. There are no limitations on the number of source code files you can work with and no limitations on the number of times you run your code. It's a great way to test a bit of Go code without having to create and compile your own local source code files.

Contents