Tutorial by Examples

println! (and its sibling, print!) provides a convenient mechanism for producing and printing text that contains dynamic data, similar to the printf family of functions found in many other languages. Its first argument is a format string, which dictates how the other arguments should be printed as t...
A simple function that does not accept any parameters and does not return any values: func SayHello() { fmt.Println("Hello!") }
A function can optionally declare a set of parameters: func SayHelloToMe(firstName, lastName string, age int) { fmt.Printf("Hello, %s %s!\n", firstName, lastName) fmt.Printf("You are %d", age) } Notice that the type for firstName is omitted because it is identical ...
A function can return one or more values to the caller: func AddAndMultiply(a, b int) (int, int) { return a+b, a*b } The second return value can also be the error var : import errors func Divide(dividend, divisor int) (int, error) { if divisor == 0 { return 0, errors.New(...
Return values can be assigned to a local variable. An empty return statement can then be used to return their current values. This is known as "naked" return. Naked return statements should be used only in short functions as they harm readability in longer functions: func Inverse(v float3...
A basic struct is declared as follows: type User struct { FirstName, LastName string Email string Age int } Each value is called a field. Fields are usually written one per line, with the field's name preceeding its type. Consecutive fields of the sa...
Struct fields whose names begin with an uppercase letter are exported. All other names are unexported. type Account struct { UserID int // exported accessToken string // unexported } Unexported fields can only be accessed by code within the same package. As such, if you are ev...
Composition provides an alternative to inheritance. A struct may include another type by name in its declaration: type Request struct { Resource string } type AuthenticatedRequest struct { Request Username, Password string } In the example above, AuthenticatedRequest will con...
Struct methods are very similar to functions: type User struct { name string } func (u User) Name() string { return u.name } func (u *User) SetName(newName string) { u.name = newName } The only difference is the addition of the method receiver. It may be declared either a...
Any function can be invoked as a goroutine by prefixing its invocation with the keyword go: func DoMultiply(x,y int) { // Simulate some hard work time.Sleep(time.Second * 1) fmt.Printf("Result: %d\n", x * y) } go DoMultiply(1,2) // first execution, non-blocking go DoMu...
single channel, single goroutine, one write, one read. package main import "fmt" import "time" func main() { // create new channel of type string ch := make(chan string) // start new anonymous goroutine go func() { time.Sleep(time.Second) ...
The filter() method accepts a test function, and returns a new array containing only the elements of the original array that pass the test provided. // Suppose we want to get all odd number in an array: var numbers = [5, 32, 43, 4]; 5.1 var odd = numbers.filter(function(n) { return n % 2 !=...
PorterDuff.Mode is used to create a PorterDuffColorFilter. A color filter modifies the color of each pixel of a visual resource. ColorFilter filter = new PorterDuffColorFilter(Color.BLUE, PorterDuff.Mode.SRC_IN); The above filter will tint the non-transparent pixels to blue color. The color fil...
An Xfermode (think "transfer" mode) works as a transfer step in drawing pipeline. When an Xfermode is applied to a Paint, the pixels drawn with the paint are combined with underlying pixels (already drawn) as per the mode: paint.setColor(Color.BLUE); paint.setXfermode(new PorterDuffXferm...
In info.plist set View controller-based status bar appearance to YES In view controllers not contained by UINavigationController implement this method. In Objective-C: - (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; } In Swift: override func pre...
Column aliases are used mainly to shorten code and make column names more readable. Code becomes shorter as long table names and unnecessary identification of columns (e.g., there may be 2 IDs in the table, but only one is used in the statement) can be avoided. Along with table aliases this allows ...
A simple literal function, printing Hello! to stdout: package main import "fmt" func main() { func(){ fmt.Println("Hello!") }() } play it on playground A literal function, printing the str argument to stdout: package main import "fmt&quot...
A variadic function can be called with any number of trailing arguments. Those elements are stored in a slice. package main import "fmt" func variadic(strs ...string) { // strs is a slice of string for i, str := range strs { fmt.Printf("%d: %s\n", i, ...
Warning: be sure you have at least 15 GB of free disk space. Compilation in Ubuntu >=13.04 Option A) Use Git Use git if you want to stay in sync with the latest Ubuntu kernel source. Detailed instructions can be found in the Kernel Git Guide. The git repository does not include necessary ...
In order to test the beginning and ending of a given string in Python, one can use the methods str.startswith() and str.endswith(). str.startswith(prefix[, start[, end]]) As it's name implies, str.startswith is used to test whether a given string starts with the given characters in prefix. >...

Page 46 of 1336