This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://leetcode.com/problems/intersection-of-two-arrays/ | |
Different ways to initialize arrays | |
==================================== | |
package main | |
import ( | |
"fmt" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
html | |
==== | |
<script src="https://facebook.github.io/react/js/jsfiddle-integration-babel.js"></script> | |
<div id="container">mm</div> | |
JS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Arrays are homogeneous—their elements all have the same type—whereas structs are heterogeneous. Both arrays and structs are fixed size. In contrast, slices and maps are dynamic data structures that grow as values are added. | |
a := [2]int{1, 2} | |
b := [...]int{1, 2} | |
c := [2]int{1, 3} | |
months := [...]string{1: "January", /* ... */, 12: "December"} | |
so January is months[1] and December is months[12]. Ordinarily, the array element at index 0 would contain the first value, but because months are always numbered from 1, we can leave it out of the declaration and it will be initialized to an empty string. | |
Arrays and slices are intimately connected. A slice is a lightweight data structure that gives access to a subsequence (or perhaps all) of the elements of an array, which is known as the slice’s underlying array. A slice has three components: a pointer, a length, and a capacity. The pointer points to the first element of the array that is reachable through the slice, which is not necessarily the array’s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Runes are printed with %c, or with %q if quoting is desired: | |
ascii := 'a' | |
unicode := 'D' | |
newline := '\n' | |
fmt.Printf("%d %[1]c %[1]q\n", ascii) // "97 a 'a'" | |
fmt.Printf("%d %[1]c %[1]q\n", unicode) // "22269 D 'D'" | |
fmt.Printf("%d %[1]q\n", newline) // "10 '\n'" | |
Float |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://openmymind.net/Introduction-To-Go-Channels/ | |
sync.Mutex | |
We've seen how channels are great for communication among goroutines. | |
But what if we don't need communication? What if we just want to make sure only one goroutine can access a variable at a time to avoid conflicts? | |
This concept is called mutual exclusion, and the conventional name for the data structure that provides it is mutex. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Method | |
====== | |
package main | |
import ( | |
"fmt" | |
"math" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
map | |
=== | |
package main | |
import ( | |
"fmt" | |
) | |
func main() { | |
mapAge := make(map[string]int) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
echo4 | |
===== | |
/* | |
var name type = expression | |
Either the type or the = expression part may be omitted, but not both. | |
If the type is omitted, it is determined by the initializer expression. | |
If the expression is omitted, | |
the initial value is the zero value for the type, which is 0 for numbers, | |
false for booleans, "" for strings, and nil for interfaces and reference types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Go online docs : https://golang.org/pkg/net/http/#Get | |
https://github.com/adonovan/gopl.io/tree/master/ch1 | |
echo1 | |
====== | |
package main | |
/* | |
import ( | |
"fmt" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Exceptions | |
========== | |
var add = function(a,b) { | |
if(typeof a !== 'number' || typeof b !== 'number') { | |
throw { | |
name : 'TypeError', | |
message: 'add needs numbers' | |
}; | |
} | |
return a+b; |