In conversation with Go-Lang | Part1

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



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
aditya goel

aditya goel

Software Engineer for Big Data distributed systems