Scopes in Go
Posted on August 16, 2020
(Last modified on August 23, 2020)
| 1 minutes
| Kinshuk Chandra
Same name cannot be declared again inside a same scope.
There are following types of scopes in Go:
package: Each Go package has it’s own scope. For e.g. declared funcs are only visible to the files belonging to same package.file: Imported packages are only visible to the importing file. Each file has to import external packages on it’s own.func.block.
Slice vs Arrays
Posted on August 16, 2020
(Last modified on August 23, 2020)
| 1 minutes
| Kinshuk Chandra
Performance Slice operations are cheap! Slicing: Creates a new slice header. Assigning a Slice to another Slice or, passing it to a function: Only copies the slice header. Slice header has a fixed size and it doesn't change even if we have got millions of elements. Array can be expensive as compared to Slice. Assigning an array to another array or passing it to a function: Copies all the elements of it.
[Read More]The internals of slice
Posted on August 16, 2020
(Last modified on August 27, 2020)
| 3 minutes
| Kinshuk Chandra
There are 3 components of slice:
a) Pointer: Points to the start position of slice in the underlying array;
b) length (type is int): the number of the valid elements of the slice;
b) capacity (type is int): the total number of slots of the slice.
Check the following code:
package main import ( "fmt" "unsafe" ) func main() { var s1 []int fmt.Println(unsafe.Sizeof(s1)) } The result is 24 on my 64-bit system (The pointer and int both occupy 8 bytes).
[Read More]Two-dimensional slice
Posted on August 16, 2020
(Last modified on August 27, 2020)
| 3 minutes
| Kinshuk Chandra
Go supports multiple-dimensional slice, but I only want to introduce two-dimensional slice here. One reason is the two-dimensional slice is usually used in daily life, while multiple-dimensional seems not common. If you often use multiple-dimensional slice, personally I think the code is a little clumsy and not easy to maintain, so maybe you can try to check whether there is a better method; the other reason is the principle behind multiple-dimensional slice is the same with two-dimensional slice, you can also understand it if you know two-dimensional slice well.
[Read More]Type assertion and type switch
Posted on August 16, 2020
(Last modified on August 23, 2020)
| 2 minutes
| Kinshuk Chandra
If we have a value and want to convert it to another or a specific type (in case of interface{}), we can use type assertion. A type assertion takes a value and tries to create another version in the specified explicit type.
This is how we can use type assertion:
x.(T) x is the variable whose type must be interface, and T is the type which you want to check. For example:
[Read More]Unused variables
Posted on August 16, 2020
(Last modified on August 23, 2020)
| 1 minutes
| Kinshuk Chandra
Unused variables in blocked scope are not allowed in Go since they cause maintenance nightmares. If we declare a variable in blocked scope then we must use it or else completely remove it from the block. We cannot have unused variables declared in blocked scope dangling in our source codes. Go throws unused variable errors at compile time only. We should avoid using package level variables. Go doesn’t throw unused variable errors at compile time for variables declared at package level.
[Read More]Use govendor to implement vendoring
Posted on August 16, 2020
(Last modified on August 27, 2020)
| 3 minutes
| Kinshuk Chandra
The meaning of vendoring in Go is squeezing a project’s all dependencies into its vendor directory. Since Go 1.6, if there is a vendor directory in current package or its parent’s directory, the dependency will be searched in vendor directory first. Govendor is such a tool to help you make use of the vendor feature. In the following example, I will demonstrate how to use govendor step by step:
(1) To be more clear, I clean $GOPATH folder first:
[Read More]Variables
Posted on August 16, 2020
(Last modified on August 27, 2020)
| 4 minutes
| Kinshuk Chandra
In Go, we have to declare a variable before we can use it. This is required and necessary for the compile time safety. Variables are not created at compile time. They are created at run time. The unnamed variables are pointers (like in C). Once we declare a type for a variable, it cannot be changed later. It is static. Declaring with var The var statement declares a list of variables with the type declared last.
[Read More]Zero Values
Posted on August 16, 2020
(Last modified on August 18, 2020)
| 1 minutes
| Kinshuk Chandra
When a variable is declared and it isn’t assigned any value at the time of declaration, Go will assign a zero value to it based on it’s variable type.
Type of a variable decides what a zero value to it will take initially when declared (and if it isn’t assigned any value at the time of declaration).
// Zero Values assigned to variables by Go when they are declared and not assigned any values at the time of declaration.
[Read More]