Skip to content

Instantly share code, notes, and snippets.

View tebeka's full-sized avatar
💭
alive

Miki Tebeka tebeka

💭
alive
View GitHub Profile
package main
import "fmt"
type Agent struct {
Name string
ID int
}
func main() {
package main
import "fmt"
func main() {
v := [...]int{2: 7}
fmt.Println(v)
}
$ echo '-2**2' | python -m ast
Module(
body=[
Expr(
value=UnaryOp(
op=USub(),
operand=BinOp(
left=Constant(value=2),
op=Pow(),
right=Constant(value=2))))],
* (expt -2 2)
4
>>> (-2)**2
4
>>> -2**2
-4

What do you think the following program will print?

https://gist.github.com/09755207f9bdfd13f71639d3e0f301cd

This program will print: 4.

When you use iota in a const group, the values of the constants will advance according to the first operation. The << operator is "left shift". It moves the bits of a number to the left, basically multiplying it by power of 2.

For example, if we have the number 5, which (on byte, which is 8 bits) is 00000101. When you write 5 << 1 all the bits are shifted one place to the left. Making it 00001010 which is 10=5*2. When you write 5 << 2 the bits are shifted two places to the left, making it 00010100 or 20=5*4.

fmt.Println(unsafe.Sizeof(filePerm)) // 1
var p Perm
fmt.Println(unsafe.Sizeof(p)) // 3
type Perm struct {
Read bool
Write bool
Execute bool
}
type FilePerm uint8
const (
Read FilePerm = 1 << iota
Write
Execute
)
func main() {
fmt.Println(Execute) // execute