From the course: Learning Go

Go's essential characteristics - Go Tutorial

From the course: Learning Go

Go's essential characteristics

- [Instructor] One of the first questions programmers frequently ask about a language is whether it's compiled or interpreted. 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. A compiled language in contrast is transformed into a format that's specific to an operating system. C and C++ are both compiled languages. 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. 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. 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. You'll see early in this course that 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. It's expected to be installed on the client 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. Another common question is whether a language is object-oriented and the answer with Go is yes, sort of. It implements some critical object-oriented features. For example, you can define custom interfaces in Go. 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. 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#. But there were other object oriented features in those 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. 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. But really it doesn't matter that much. As long as you understand the fundamentals of programming, you should find Go to be a very learnable language.

Contents