(with the go interpreter/REPL yaegi)
> s := []int{1, 2, 4}
: [1 2 4]
> s[0]
: 1
> s = append(s, 8)
: [1 2 4 8]
> for i, n := range s { println(i, n) }
0 1
1 2
2 4
3 8
: [1 2 4 8]
>>> d = {"a": 1, "b": 2}
>>> d["a"]
1
>>> d["c"] = 3
>>> d
{'a': 1, 'b': 2, 'c': 3}
>>> for k, v in d.items():
... print(k, v)
...
a 1
b 2
c 3
> m := map[string]int{"a": 1, "b": 2}
: map[a:1 b:2]
> m["a"]
: 1
> m["c"] = 3
: 0
> m
: map[a:1 b:2 c:3]
> for k, v := range m { println(k, v) }
a 1
b 2
c 3
: map[a:1 b:2 c:3]
class Person:
def __init__(self, name, year):
self.name = name
self.year = year
def main():
guido = Person(name="Guido van Rossum", year=1956)
print(guido.name)
print(guido.year)
package main
type Person struct {
Name string
Year int
}
func main() {
rob := Person{Name: "Robert Pike", Year: 1956}
println(rob.Name)
println(rob.Year)
}
from datetime import date
class Person:
def __init__(self, name, year):
self.name = name
self.year = year
def get_age(self):
return date.today().year - self.year
def main():
guido = Person(name="Guido van Rossum", year=1956)
print(guido.name)
print(guido.year)
print(guido.get_age())
package main
import "time"
type Person struct {
Name string
Year int
}
func (p Person) Age() int {
return time.Now().Year() - p.Year
}
func main() {
rob := Person{Name: "Robert Pike", Year: 1956}
println(rob.Name)
println(rob.Year)
println(rob.Age())
}
...
func (p Person) SetName(name string) {
p.Name = name
}
func (p Person) SetYear(year int) {
p.Year = year
}
func main() {
rob := Person{} // equivalent to Person{Name: "", Year: 0}
rob.SetName("Robert Pike")
rob.SetYear(1956)
println(rob.Name)
println(rob.Year)
println(rob.Age())
}
$ go run app.go
0
2024
Uhu?
What is the semantics of assignment ?
sum = 1 + 1
Evaluate the right-hand side
???
Profit!
Variables refer to a location in memory (via a pointer).
Assignment rebinds the pointer to a new location.
>>> a = 1 + 1
>>> print(hex(id(a)))
0x559b96062d60
>>> a = 42
>>> print(hex(id(a)))
0x559b96063260
Variables store data in a fixed location:
package main
func main() {
a := 1 + 1
println(&a)
a = 42
println(&a)
}
Output:
0xc00003e728
0xc00003e728
We can also store content at a location
package main
func main() {
a := 1 + 1
p := &a
*p = 42
println(a)
}
Output:
42
func (p *Person) SetName(name string) {
p.Name = name
}
func (p *Person) SetYear(year int) {
p.Year = year
}
func main() {
rob := Person{}
rob.SetName("Robert Pike")
rob.SetYear(1956)
println(rob.Name)
println(rob.Year)
println(rob.Age())
}
$ go run app.go
Robert Pike
1956
68
$ go mod init app
Crรฉe le fichier go.mod
:
module app
go 1.25.1
person.go
package main
import "time"
type Person struct {
Name string
Year int
}
func (p Person) Age() int {
return time.Now().Year() - p.Year
}
func (p *Person) SetName(name string) {
p.Name = name
}
func (p *Person) SetYear(year int) {
p.Year = year
}
app.go
package main
func main() {
rob := Person{}
rob.SetName("Robert Pike")
rob.SetYear(1956)
println(rob.Name)
println(rob.Year)
println(rob.Age())
}
On my (Linux) computer,
$ go build
creates a app
executable for Linux.
$ ./app
Robert Pike
1956
68
Set the environment variables GOOS
and GOARCH
, then go build
as usual:
GOOS=windows
and GOARCH=amd64
app.exe
.
GOOS=darwin
GOARCH=arm64
app
.
GOOS=linux
and GOARCH=arm64
app
.
Try TinyGo!
(Arduino, Raspberry Pi Pico, etc.)