Skip to content

Instantly share code, notes, and snippets.

@cozingo
Last active November 18, 2022 15:31
Show Gist options
  • Save cozingo/dd5edf0e4b9bbbd008522eebe4086dbf to your computer and use it in GitHub Desktop.
Save cozingo/dd5edf0e4b9bbbd008522eebe4086dbf to your computer and use it in GitHub Desktop.
go basic
f := "apple"
//is same as
var f = "apple"
//is same as
var f string = "apple"
operators : &&, ||
Closure
func intSeq() func() int {
i := 0
return func() int {
i++
return i
}
}
nextInt := intSeq()
////////////////////////////
Interfaces are named collections of method signatures.
type geometry interface {
area() float64
perim() float64
}
/////////////////////////
errors.New("can't work with 42")
1. Outside a function, every statement begins with a keyword (var, func, and so on) and so the := construct is not available.
2. Constants cannot be declared using the := syntax.
1. make -> make([]string, 3)//type, length
creates array of same types
2. append -> append(s, "d")//array, new value(s) -> append(s, "e", "f")
appends to the array the value
3. copy -> copy(c, s)//copying (to,from)
4. delete -> delete(m, k)//delete from m entry with the key of 'k'
slice[start:end]
[2:5] -> [2, 3, 4]
[:5] -> [0:up to 5th]
[2:] -> [2:up to end]
/////////////////////////////////////
range iterates over arrays, maps, strings
for _, num := range nums {
sum += num
}
//////////////////////////////////////
func plus(a type, b type) type {
return a + b
}
func split(sum int) (x, y int) {
x = sum * 4 / 9
y = sum - x
return
}
//same as return x, y. No need to declare vars at the top of the function
func vals() (int, int) {
return 3, 7
}
Variadic functions can be called with any number of trailing arguments
func sum(nums ...int) {
total := 0
for _, num := range nums {
total += num
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment