Tutorial by Examples

The zero value of a map is nil and has a length of 0. var m map[string]string fmt.Println(m == nil) // true fmt.Println(len(m) ==0) // true A nil map has no keys nor can keys be added. A nil map behaves like an empty map if read from but causes a runtime panic if written to. var m map[string]...
import fmt people := map[string]int{ "john": 30, "jane": 29, "mark": 11, } for key, value := range people { fmt.Println("Name:", key, "Age:", value) } Note that when iterating over a map with a range loop, the iteration order i...
people := map[string]int{ "john": 30, "jane": 29, "mark": 11, } for key, _ := range people { fmt.Println("Name:", key) } If you are just looking for the keys, since they are the first value, you can simply drop the underscore: for key := r...
The delete built-in function removes the element with the specified key from a map. people := map[string]int{"john": 30, "jane": 29} fmt.Println(people) // map[john:30 jane:29] delete(people, "john") fmt.Println(people) // map[jane:29] If the map is nil or ther...
Go programs end when the main function ends, therefore it is common practice to wait for all goroutines to finish. A common solution for this is to use a sync.WaitGroup object. package main import ( "fmt" "sync" ) var wg sync.WaitGroup // 1 func routine(i int...
slice = append(slice, "hello", "world")
You shouldn't call NSLog without a literal format string like this: NSLog(variable); // Dangerous code! If the variable is not an NSString, the program will crash, because NSLog expects an NSString. If the variable is an NSString, it will work unless your string contains a %. NSLog will pars...
A useful tool in Java Concurrency is ThreadLocal – this allows you to have a variable that will be unique to a given thread. Thus, if the same code runs in different threads, these executions will not share the value, but instead each thread has its own variable that is local to the thread. For exa...
To create a first-level (<h1>) header, use the equal sign (=) in a line under your text: All About Dogs ============== All About Dogs Use hyphens (-) for second-level (<h2>) headers: The Debut Novel --------------- The Debut Novel The line below the header can be of a...
When two margins are touching each other vertically, they are collapsed. When two margins touch horizontally, they do not collapse. Example of adjacent vertical margins: Consider the following styles and markup: div{ margin: 10px; } <div> some content </div> <div&gt...
We can illustrate this problem with the following pseudo-code function foo() { global $bob; $bob->doSomething(); } Your first question here is an obvious one Where did $bob come from? Are you confused? Good. You've just learned why globals are confusing and considered a bad p...
Bool is a Boolean type with two possible values: true and false. let aTrueBool = true let aFalseBool = false Bools are used in control-flow statements as conditions. The if statement uses a Boolean condition to determine which block of code to run: func test(_ someBoolean: Bool) { if som...
The prefix ! operator returns the logical negation of its argument. That is, !true returns false, and !false returns true. print(!true) // prints "false" print(!false) // prints "true" func test(_ someBoolean: Bool) { if !someBoolean { print("someBoolean ...
Creating immutable arrays: NSArray *myColors = [NSArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil]; // Using the array literal syntax: NSArray *myColors = @[@"Red", @"Green", @"Blue", @"Yellow"]; ...
NSArray *myColors = [NSArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil]; NSLog (@"Number of elements in array = %lu", [myColors count]);
NSMutableArray *myColors; myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil]; [myColors addObject: @"Indigo"]; [myColors addObject: @"Violet"]; //Add objects from an NSArray NSArray *myArray = @[@...
NSMutableArray *myColors; int i; int count; myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil]; [myColors insertObject: @"Indigo" atIndex: 1]; [myColors insertObject: @"Violet" atIndex: 3];
Remove at specific index: [myColors removeObjectAtIndex: 3]; Remove the first instance of a specific object: [myColors removeObject: @"Red"]; Remove all instances of a specific object: [myColors removeObjectIdenticalTo: @"Red"]; Remove all objects: [myColors removeAl...
NSArray *myColors = @[@"Red", @"Green", @"Blue", @"Yellow"]; // Preceding is the preferred equivalent to [NSArray arrayWithObjects:...] Getting a single item The objectAtIndex: method provides a single object. The first object in an NSArray is index 0. Si...
NSMutableArray *myColors = [NSMutableArray arrayWithObjects: @"red", @"green", @"blue", @"yellow", nil]; NSArray *sortedArray; sortedArray = [myColors sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

Page 94 of 1336