What is go programming?
Go is an open source or general-purpose programming language with advanced more features and easy understanding small syntax. Go is also a great language for writing concurrent programs. Programs with many independently running parts. Go is deliberately not an object-oriented language: There are no classes, objects, or inheritance. Instead, we’ll declare a struct type called Fund, with a simple function to create new fund structs, and two public methods.
Why “go” programming?
Go programming language has different features that make it attractive and special. Here is a not limited list:
- Go programming has the good tooling and a great standard library that helps you build robust programs. The standard Go library is very complete. Almost everything has a built-in package. HTTP servers to clients, cryptography, testing, managing file systems, and more.
- Go programming is fast. And not only fast in the sense that programs written in it run fast when compared to other common languages; but also fast in the sense that its compiler can compile projects in the blink of an eye. So much so that it makes Go feels like an interpreted language instead of a compiled one. You can even edit and run Go programs directly on the Web.
- Go has a rich standard library which covers a lot of areas. In fact, Go is probably the only language that can claim to have a fully working Web server as part of its standard library.
- Although it is compiled, Go is also a garbage-collected language. This puts less pressure on the developer to do memory management, as the language itself takes care of most of the grunt work needed. In this respect, it is similar to Java, as opposed to C++.
- Golang has built-in concurrency, which allows parallelism in an easier way than is possible in other languages. Go has the concept of goroutines to start concurrent work and the concept of channels to permit both communication and synchronization.
- It is a statically-typed language (which makes code more robust as several possible bugs can be detected during compilation) that supports duck-typing (types can satisfy interfaces and be passed in to any functions/methods that accept those interfaces). Go also has type inference, which means you do not need to explicitly mention a variable type whenever it can be inferred by the compiler.
- Go’s built-in build system is both elegant and simple. No need to mess with build configurations or make files.
What is syntax of go programming? Or Hello world program in Go?
First create a new folder where we can store our program. Create a folder named ~/src/golang-notes/section 1. (Where ~ means your home directory) From the terminal you can do this by entering the following commands:
mkdir src/golang-notes
mkdir src/golang-notes/section 1
Using your text editor type in the following:
package main
import “fmt”
// this is a comment
func main() {
fmt.Println(“Hello World”)
}
Make sure your file is identical to what is shown here and save it as main.go in the folder we just created. Open up a new terminal and type in the following:
cd src/golang-notes/section 1
go run main.go
You should see Hello World displayed in your terminal.
Does Go programming support type inheritance and Overloading and method overloading and arithmetic’s and generic programming?
Go programming language doesn’t support or No support for type inheritance, Overloading, method overloading, arithmetic’s and generic programming.
What is workspace in Go programming?
A workspace contains Go code. A workspace is a directory hierarchy with three directories at its root.
- “src” directory contains GO source files organized into packages.
- “pkg” directory contains package objects.
- “bin” directory contains executable commands
What are the lvalues and the rvalues in Go programming?
There are two kinds of expressions in Go:
lvalue: Expressions that refer to a memory location is called “lvalue” expression. An lvalue may appear as either the left-hand or right-hand side of an assignment.
rvalue: The term rvalue refers to a data value that is stored at some address in memory. An rvalue is an expression that cannot have a value assigned to it which means an rvalue may appear on the right- but not left-hand side of an assignment.
What is variable in Go programming?
A variable definition tells the compiler where and how much storage to create for the variable. A variable definition specifies a data type and contains a list of one or more variables of that type as follows: var variable_list optional_data_type; Here, optional_data_type is a valid Go data type including byte, int, float32, complex64, Boolean or any user-defined object, etc.,
What is a string literal in Go?
A string literal specifies a string constant that is obtained from concatenating a sequence of characters. There are two types of string literals:
Raw string literals: The value of raw string literals are character sequence between back quotes “. Its value is specified as a string literal that composed of the uninterrupted character between quotes.
Interpreted string literals: It is shown between double quotes ” “. The value of the literal is specified as text between the double quotes which may not contain newlines.
What is static type declaration and dynamic type declaration of a variable in Go?
Static type declaration: Static type variable declaration provides assurance to the compiler that there is one variable existing with the given type and name so that compiler proceed for further compilation without needing complete detail about the variable. A variable declaration has its meaning at the time of compilation only, compiler needs actual variable declaration at the time of linking of the program.
Dynamic type declaration: A dynamic type variable declaration requires compiler to interpret the type of variable based on value passed to it. Compiler don’t need a variable to have type statically as a necessary requirement.
What are types in go programming?
The following are the basic types available in go
Boolean types: A Boolean value (named after George Boole) is a special 1 bit integer type used to represent true and false. Three logical operators are used with Boolean values:
Numbers types: Go has several different types to represent numbers. Generally we split numbers into two different kinds: integers and floating-point numbers.
- int8, int16, int32, int64, int
- uint8, uint16, uint32, uint64, uint
- float32, float64
- complex64, complex128
- byte
- rune
Strings types: Go strings are made up of individual bytes, usually one for each character. String literals can be created using double quotes “Hello World” or back ticks `Hello World`. The difference between these is that double quoted strings cannot contain newlines and they allow special escape sequences. For example, \n gets replaced with a newline and \t gets replaced with a tab character.
Derived types: They include Pointer types, Array types, Structure types, Union types, Slice types, Interface types, Map types, and Channel Types.
What is a pointer in Go programming?
A pointer is used to hold the address of a variable.
What is the purpose of break statement?
Break terminates the for loop or switch statement and transfers execution to the statement immediately following the for loop or switch.
What is a structure in go?
A structure is a type which contains named fields. Or a structure is a user defined type which represents a collection of fields. Structures are used to represent a record. It can be used in places where it makes sense to group the data into a single unit rather than maintaining each of them as separate types.
What is an interface in go?
“Interface defines the behavior of an object”. In Go, an interface is a set of method signatures. When a type provides definition for all the methods in the interface, it is said to implement the interface. It is much similar to the OOP world. Interface specifies what methods a type should have and the type decides how to implement these methods.
What is testing in go?
Go includes a special program that makes writing tests easier. The go test command will look for any tests in any of the files in the current folder and run them. Tests are identified by starting a function with the word Test and taking one argument of type *testing.T.
What is a function in go programming?
A function is a block of code that performs a specific task. A function takes a input, performs some calculations on the input and generates a output.
Function declaration:
The general syntax for declaring a function in go is
func functionname(parametername type) returntype {
//function body
}
What is Modular Programming?
Dividing the program in to sub programs (modules/function) to achieve the given task is modular approach. More generic functions definition gives the ability to re-use the functions, such as built-in library functions.
What is a token?
A Go program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol.
What is a nil Pointers in Go?
Go language compiler assign a nil value to a pointer variable in case you do not have exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned nil is called a nil pointer. The nil pointer is a constant with a value of zero defined in several standard libraries.
What is panic? When should panic be used?
The idiomatic way to handle abnormal conditions in a program in Go is using errors. Errors are sufficient for most of the abnormal conditions arising in the program. One important factor is that you should avoid panic and recover and use errors where ever possible. Only in cases where the program just cannot continue execution should a panic and recover mechanism be used.
There are two valid use cases for panic.
- An unrecoverable error where the program cannot simply continue its execution.
One example would be a web server which fails to bind to the required port. In this case it’s reasonable to panic as there is nothing else to do if the port binding itself fails.
- A programmer errors. Let’s say we have a method which accepts a pointer as a parameter and someone calls this method using nil as argument. In this case we can panic as it’s a programmer error to call a method with nil argument which was expecting a valid pointer.
How to delete an entry from a map in Go programming?
The delete () function is used to delete an entry from the map and it’s requires map and corresponding key which is to be deleted.
What is Defer in go?
Defer statement is used to execute a function call just before the function where the defer statement is present returns. Defer is not restricted only to functions. It is perfectly legal to defer a method call too. Defer is used in places where a function call should be executed irrespective of the code flow.
What is slice in Go programming?
A slice is a convenient, flexible and powerful wrapper on top of an array. Slices do not own any data on their own. They are the just references to existing arrays. (Or)Go Slice is an abstraction over Go Array. As Go Array allows you to define type of variables that can hold several data items of the same kind but it do not provide any inbuilt method to increase size of it dynamically or get a sub-array of its own. Slices covers this limitation. It provides many utility functions required on Array and is widely used in Go programming.
What is channels?
Channels can be thought as pipes using which Goroutines communicate. Similar to how water flows from one end to another in a pipe, data can be sent from one end and received from another end using channels.
What is range in Go programming?
The range keyword is used in for loop to iterate over items of an array, slice, channel or map. With array and slices, it returns the index of the item as integer. With maps, it returns the key of the next key-value pair.
What is maps in Go programming?
A map is a built-in type in Go which associates a value to a key. The value can be retrieved using the corresponding key. Go provides another important data type map which maps unique keys to values. A key is an object that you use to retrieve a value at a later date. Given a key and a value, you can store the value in a Map object. After value is stored, you can retrieve it by using its key.
What is recursion?
Recursion is the process of repeating items in a self-similar way. The same concept applies in programming languages as well. If a program allows to call a function inside the same function, then it is called a recursive function call
What is the difference between cap () and len () in go?
Len (): Ggives the number of elements inside a slice.
Cap (): Gives the capacity of the slice. Number of elements the slice can accommodate.
What is Type casting in go programming?
Type casting is a way to convert a variable from one data type to another data type. For example, if you want to store a long value into a simple integer then you can type cast long to int. You can convert values from one type to another using the cast operator. Its syntax is as follows:
type_name(expression)
What is Goroutin?
Goroutin is a function or method that run concurrently with other functions or methods. Goroutines can be thought of as light weight threads. The cost of creating a Goroutine is tiny when compared to a thread. Hence, it’s common for Go applications to have thousands of Goroutines running concurrently. Goroutines communicate using channels. Channels by design prevent race conditions from happening when accessing shared memory using Goroutines.
What is range in go programming?
The range keyword is used in for loop to iterate over items of an array, slice, channel or map. With array and slices, it returns the index of the item as integer. With maps, it returns the key of the next key-value pair. Range either returns one value or two. If only one value is used on the left of a range expression, it is the 1st value in the following table.
Range expression 1st Value 2nd Value (Optional)
Array or slice a [n]E index i int a[i] E
String s string type index i int rune int
map m map[K]V key k K value m[k] V
channel c chan E element e E none
What is Gopath?
The GOPATH environment variable is used to specify directories outside of $GOROOT that contain the source for Go projects and their binaries.
Why won't $GOPATH/src/cmd/mycmd/*.go build in go programming?
Go command is looking for packages, it always looks in $GOROOT first. This includes directories, so if it finds (as in the case above) a cmd/ directory in $GOROOT it won’t proceed to look in any of the GOPATH directories. This prevents you from defining your own math/matrix package as well as your own cmd/mycmd commands.
What are the different built-in supports in Go programming?
There are built-in supports in Go programming:
- Database: database/sql
- Container: container/list, container/heap
- Web Server: net/http
- Cryptography: Crypto/md5, crypto/sha1
- Compression: compress/ gzip