In conversation with Go-Lang | Part1

aditya goel
12 min readDec 5, 2021

In this blog, we shall be looking at following concepts :-

  • Best example of Interpreted Language.
  • Modus Operandi for Compiled Languages.
  • GoLang a Compiled or Interpreted language.
  • Deliver applications built in Go.
  • Runtime component in Go.
  • Size of compiled application compared to source code in Java.
  • Is Go an Object oriented language.
  • Can we define Interfaces in Go ?
  • Can we define structures in Go ?
  • Object oriented features not presented in Go.
  • Syntax rules in Go.
  • Advantages & Limitations of Go-Playground.

Question: What’s the best example of an Interpreted Language ?

Answer: An example of an interpreted language is JavaScript. JavaScript’s source code is read directly by the browser when you’re running in the browser and then executed at runtime. There is no precompilation step and while in some interpreted environments there’s an intermediate format such as bytecode, there’s no precompilation step that you have to follow.

Question: What’s the modus operandi for compiled languages ?

Answer: A compiled language in contrast is transformed into a format that’s specific to an operating system. C and C++ are both compiled languages.

Question: Is GO-Lang a compiled or interpreted language ?

Answer: Go is a compiled language. Unlike Java which is compiled to bytecode that can run on multiple operating systems, Go is compiled to a form that can only run on a single operating system and it’s also statically typed, which means that its variables have specific types. You don’t always have to explicitly declare the types. There are sometimes inferred by the compiler. But they’re always known at compilation time. It can sometimes feel with Go like you’re working with an interpreted language because you can run a source code file without precompiling. But what’s really happening in the background is that the application is being compiled to a temporary executable.

Question: How do we deliver applications built in GO ?

Answer: When you’re creating applications that you’re going to deliver to users though, you’ll normally use the compilation tool known simply as Go, and as I mentioned, the compiled executable will only run on one operating system. So if you compile for Windows, that executable can only be run on Windows. If you compile for Mac, it can only run on Mac and so on.

Question: Is there a RunTime Component associated with GO ?

Answer: Applications built with Go contain a statically linked runtime. That is there’s a run-time component that’s packaged with the application during the compilation process.

Question: Usually, how is the size of compiled application compared to the Source code in Java & GO?

Answer: The size of a compiled application is much larger than its source code file and that’s because that runtime is being included. But there is no external virtual machine. So for example, in Java, you have an operating system specific virtual machine or JVM. That JVM is mandatory and expected to be installed on the client-machine and therefore the Java application which is compiled into bytecode can be very, very small. With Go, there is no external virtual machine and so that runtime has to be included in every compiled application.

Question: Is the GO an Object-oriented language ?

Answer: The answer with Go is yes, sort of. It implements some critical object-oriented features. For example, you can define custom interfaces in Go.

Question: Can we define the Interfaces defined in GO as well ?

Answer: An interface is a contract of sorts that defines a set of functions and then something that implements that interface has to implement those functions. In Go you can define types. Almost everything is a type in Go and every type implements at least one interface. When you add functions to a type in Go, they are now called methods and that’s an object-oriented term.

Question: Can we create our own Structures in GO as well ?

Answer: You can create your own structures or structs in Go and those can have member methods and member fields. So the types and structures of Go can feel a lot like the classes in a language such as Java or C#.

Question: What are those object oriented features, which are not present in GO ?

Answer:

  • There were other object oriented features in other languages that aren’t present in Go. For example, there is no type inheritance in Go. You can’t create a type and say that’s going to be the super type and then create another something that’s called a subtype and inherit features from the super type. That’s just not a part of the Go architecture.
  • There’s also no method or operator overloading in Go. Each function or method within package or type in Go has a specific signature but you can’t have more than one function in a package or more than one method in a type that has the same name.
  • Also, Go doesn’t have structured exception handling such as you might expect in a language like C or C#. You don’t have the try, catch, or finally key words that appear in those other languages. Instead, error objects are simply returned by functions which might generate those errors and then you use conditional logic to examine the error objects.
  • There are also no implicit numeric conversions. You have to explicitly type every variable or implicitly type it by saying exactly where you’re getting the data. If you want to convert a value from one type to another in Go, you have to do it explicitly by wrapping the value in a function that says I want it to be this type.

The main reason you won’t find these language features in Go is because the languages designers felt that these features which are common to very advanced languages make these languages harder to read and more susceptible to bugs. Everything you need to know about a Go program is right there on the surface. You don’t have to remember a rule of the language because it’s all there in the application’s code.

Question: How is GO formed and what all can we do with GO ?

Answer: Go is based on a number of different languages. It was originally designed as a next-generation language that could do everything you can do with C. Systems programming, application development, and so on. So it borrows a lot of syntax from C and it’s related languages, C++, C#, Java, and so on, but it also borrows syntax from other languages such as Pascal, Modula, Oberon, and other similar languages. At the end of the day, one of the attractions of Go is that you simply don’t have to do as much typing. Go programming is very concise and there aren’t a lot of unnecessary characters to get in the way. Now if you’re already an expert in one of those C-style languages, you will be ahead of the game with Go.

Question: What are syntax-rules in GO ?

Answer: Here are some critical syntax rules in Go :-

  • Go is case sensitive. So identifiers such as function, variable and type names, have to be spelled exactly the way you see them in the documentation.
  • Most variables and package names are lower and mixed case, but methods, which are functions that belong to types and fields, which are variables that are members of types frequently have initial uppercase characters. That has a special meaning in Go.
  • An initial uppercase character, means that that symbol is exported. And that’s the equivalent of the keyword public in other languages like Java and C sharp, a lowercase initial character means that the field or method isn’t exported and isn’t available to the rest of the application, or you could say that it’s private.
  • Another important aspect of Go, is that it tries to reduce the amount of typing you have to do as a developer. And one of the ways it does this is to eliminate semi-colons from the ends of lines.
  • Now, interestingly, the language specification says that semi-colons are supposed to be there, but you don’t have to type them. And that’s because the lexer, which is the software component that parses your code and analyzes it, adds the semi-colons during the compilation process as needed. In order to accomplish this, Go is sensitive to white space. That is line feeds, tabs, and so on.
  • There is a fairly simple rule that dictates when the semi-colons are added for you. If a statement is determined to be complete and the lexer encounters a line feed, that means it’s the end of the statement, and you don’t need to add the semi-colon yourself. But that also means, that you can’t always add line feeds freely or break up statements with extra line feeds as you might do in other languages, because in certain circumstances that can be misinterpreted by the lexer.
  • As with other C style languages code blocks. That is multiple lines of code that are intended to be grouped together are wrapped with brace characters. Here’s an example, I’m declaring a variable named some, and then I moved being from zero to 10 and I’m incrementally adding the value each time. And then I’m outputting that value using a function called print LN or print line. That’s a member of a package called FMT for format, and it outputs the value of 45.
  • In a four statement, the code that you want to iterate over is wrapped between those two braces now because of the rules of how the lexer analyses new lines, the first brace and a code block must be on the same line as any proceeding statement. Unlike, other C style languages that aren’t sensitive to white space, in Go, you don’t have the freedom to drop that beginning brace down to the next blank line.
  • Unlike in Java, where every method is a member of a class, Go has a set of built-in functions that are always available in your code without having to import anything. These functions are members of a special package named builtin. The Go compiler assumes that the builtin package is always imported. You’ll find functions like len for length, panic, to stop execution and displaying an error message, and recover to manage the behavior of a panicking routine.

You can learn about these and other built-in functions and types at this page in the online documentation, @golang.org/pkg/builtin.

Question: How can we try our hands dirty with Go-Lang ?

Answer: 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.

Next, Let’s 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.

Question: What’s happening in the background, upon pressing RUN button?

Answer:

  • In the background the Go Playground is simply using the Go Command with formatting commands.
  • 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.

Question: With Go-Playground, what are the other advantages/limitations ?

Answer:

  • 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.

Question: With Go-Playground, Are there any limitations ?

Answer: 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.

Question: With Go-Playground, Can I share my code with my peers ?

Answer: Yes, 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.

Question: Can you explain the basic syntax of the Go-Code-sample shown above ?

Answer:

  • Every source-code-file developed in the GO language must have .go extension. The name of the file before the file extension DOE could be anything you like, but once again, it should be all lowercase and no spaces.
  • In order to serve as the startup code for an application, this file must be a member of a package called main. So you can observe, we had added that code at the top of the file.
  • Next I’ll need imports for any packages I’m going to use in my application. I’ll start with the keyword import and if I’m only going to be using one package, I can do it all on a single line. I’m going to be using a package called, fmt for formatting.
  • I’ve already added this file to the main package and the other requirement for it to serve as a start-up file is that there must be a function named, main. All functions in go, start with the func keyword.
  • The function names start with a lowercase character if they’re private to this file or start with an uppercase character, if they’re public.
  • I’ll add a pair of braces and then I’ll call fmt and I’ll call one of the functions from this package, Print ln. And I’ll pass in a string of, Hello from Go. I’ll save my changes by pressing Command + S on Mac, and a couple of things will happen.
  • First of all, in the background the Go compiler examines the file and determines whether there are any syntax errors. Also, if you have any unused imports at the top of the file, you may see them go away, that’s normal behaviour.
  • V.Imp Point → Please note that, within a single application, only one source code file can have a main function. So all you need to do is designate which folder you’re working with and the compiler figures out everything else. And once again, I see the output.
  • Next, say I want to package the runtime and build a binary file that I can distribute, I can do this, Go build., I don’t see any output, but now if I list the contents of this directory I see a new file named, hello. It has the same name as the folder in which it’s contained. If I then type, ./hello, I’m running the compiled version. And you may notice that it’s a lot faster than running that application with Go run.
  • This is now a compiled binary file, specifically designed to work on my operating system. If I take that file and copy it over to Windows, it isn’t going to work. And if you’re working on Windows, instead of seeing just, hello, you’ll see, hello.exe. It’s also possible to compile for other operating systems, but by default you’ll get a binary for your operating system.

References :-

--

--