created the project under $GOPATH
Not working, but in progres 2 5 3 4
import "fmt"
type P struct {
x string
y int
}
func main() {
b := P{"x", -1}
a := [...]P{P{"a", 10},
P{"b", 2},
P{"c", 3}}
for _, z := range a {
if z.y > b.y {
b = z
}
}
fmt.Println(b.x)
}
import "fmt"
func main() {
s := make([]int, 0, 3)
s = append(s, 100)
fmt.Println(len(s), cap(s))
}
{“name” : “joe”, “addr”:”a st.”, “phone”:”123”}
- All Unicode
- Human-readable
- Fairly compact representation
- Types can be combined recursively
- Array of structs, struct in struct, etc.
package main
import (
"encoding/json"
"fmt"
)
type Person struct {
name string
addr string
phone string
}
func main() {
p1 := Person{name: "joe", addr: "a st.", phone: "123"}
barr, err := json.Marshal(p1)
fmt.Println(err)
fmt.Println("byte array (barr):", barr, "\t person 1 (p1) in Go:", p1)
var p2 Person
err2 := json.Unmarshal(barr, &p2)
fmt.Println("person 2 (p2) in Go:", p2, "error:", err2)
}
package main
import (
"encoding/json"
"fmt"
)
type Person struct {
Name string `json:"name"`
Addr string `json:"addr"`
Phone string `json:"phone"`
}
func main() {
p1 := Person{Name: "joe", Addr: "a st.", Phone: "123"}
barr, err := json.Marshal(p1)
if err != nil {
panic(err)
}
fmt.Println(string(barr))
var p2 Person
err2 := json.Unmarshal(barr, &p2)
fmt.Println("person 2 (p2) in Go:", p2, "error:", err2)
}
package main
import ("fmt"
"os")
func main() {
f, err := os.Open("dt.txt")
fmt.Println(f, err)
barr := make([]byte, 10000)
nb, err := f.Read(barr)
fmt.Println(string(nb[:10]), err)
f.Close()
}
package main
import (
"fmt"
"os"
"bufio"
)
func main() {
file, err := os.Open("dt.txt")
if err != nil {
fmt.Println(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Println(err)
}
}
package main
import (
"encoding/json"
"fmt"
)
type Person struct {
Name string `json:"name"`
Address string `json:"address"`
}
func main() {
var x string
fmt.Println("Enter 'Name':")
_, _ = fmt.Scan(&x)
var y string
fmt.Println("Enter 'Address':")
_, _ = fmt.Scan(&y)
p1 := Person{Name: x, Address: y}
barr, err := json.Marshal(p1)
if err != nil {
panic(err)
}
fmt.Println(string(barr))
}
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
type T struct {
fname string
lname string
}
func main() {
// Ask for file name (fn)
fmt.Println("Enter file name, e.g.: 'names.txt'")
var fn string
_, _ = fmt.Scan(&fn)
// Open file
file, err := os.Open(fn)
if err != nil {
fmt.Println(err)
}
defer file.Close()
// Create a slice, in which the T constructs will be populated.
var slice []T
// Counter for each line
i := 0
// Read each line and iterate on it
scanner := bufio.NewScanner(file)
for scanner.Scan() {
fmt.Println(scanner.Text())
strline := scanner.Text()
// words := strings.Split(strline, " ")
// var words [20]string
words := strings.Fields(strline)
// fmt.Println(words, len(words))
fmt.Println("First name:", words[0], "Last name:", words[1])
slice = append(slice, T{fname: words[0], lname: words[1]})
fmt.Println("Slice so far:", slice)
i = i + 1
}
if err := scanner.Err(); err != nil {
fmt.Println(err)
}
// return slice with T-type elements
fmt.Println(slice)
// Iterate and return first and last names
fmt.Println("List of first and last names in the file:")
for _, value := range slice {
fmt.Println("First name:", value.fname, "Last name:", value.lname)
}
package main
import (
"flag"
"fmt"
"strconv"
)
func Swap(first, next, count int) (int, int, int) {
if first > next {
first, next = next, first
count = count + 1
}
return first, next, count
}
func BubbleSort(sli []int) []int {
count_sort := 0
for i := 0; i < len(sli)-1; i++ { // sort two-by-two, all 2-tuples
sli[i], sli[i+1], count_sort = Swap(sli[i], sli[i+1], count_sort)
}
if count_sort != 0 {
BubbleSort(sli)
}
return sli
}
func useFlags() []int {
flag.Parse()
args := flag.Args()
fmt.Println("args:", args)
nums := make([]int, len(args))
// for each argument, insert in position nums[i] the string-converted value num64
for i, arg := range args {
num64, err := strconv.ParseInt(arg, 0, 0)
if err != nil {
fmt.Println("You probably didn't enter only integers.")
}
nums[i] = int(num64)
}
return nums
}
func main() {
unsorted := useFlags()
sorted := BubbleSort(unsorted)
fmt.Println(sorted)
}
package main
import (
"flag"
"fmt"
"strconv"
)
func Swap(sli []int, i int) bool {
if sli[i] > sli[i+1] {
sli[i], sli[i+1] = sli[i+1], sli[i]
return true
} else {
return false
}
}
func BubbleSort(sli []int) []int {
count_sort := 1
for count_sort > 0 {
count_sort = 0
for i := 0; i < len(sli)-1; i++ { // sort two-by-two, all 2-tuples
if Swap(sli, i) {
count_sort++
}
}
if count_sort != 0 {
BubbleSort(sli)
}
}
return sli
}
func useFlags() []int {
flag.Parse()
args := flag.Args()
fmt.Println("args:", args)
nums := make([]int, len(args))
// for each argument, insert in position nums[i] the string-converted value num64
for i, arg := range args {
num64, err := strconv.ParseInt(arg, 0, 0)
if err != nil {
fmt.Println("You probably didn't enter only integers.")
}
nums[i] = int(num64)
}
return nums
}
func main() {
unsorted := useFlags()
sorted := BubbleSort(unsorted)
fmt.Println(sorted)
}
Declaring a func
as var
import "fmt"
var funVar func(int) int
func incFn(x int) int {
return x+1
}
func main (){
funVar = incFn
fmt.Println(funVar(1))
fmt.Println(incFn)
}
import "fmt"
var funVar func(int) int
func incFn(x int) int {
return x+1
}
func applyIt(f func (int) int, x int) int {
return f(x)
}
func main (){
funVar = incFn
fmt.Println(funVar(1))
fmt.Println(incFn)
fmt.Println(applyIt(incFn,1))
fmt.Println(applyIt(incFn,(applyIt(incFn,1))))
}
import "fmt"
func useFlags() [3]float64 {
flag.Parse()
args := flag.Args()
fmt.Println("args:", args)
nums := make([]int, len(args))
// for each argument, insert in position nums[i] the string-converted value num64
for i, arg := range args {
num64, err := strconv.ParseFloat(arg, 0, 0)
if err != nil {
fmt.Println("You probably didn't enter only integers.")
}
nums[i] = int(num64)
}
return nums
}
func GenDisplaceFn(a, v0, s0 float64) func(float64){
space := func (t float64){
return (1/2)*a*t*t + v0*t + s0
}
return space
}
func main (){
// fmt.Println("Enter a acceleration (a), initial velocity (v0) and (s0):")
var params [3]float64
params = useFlags()
fn := GenDisplaceFn(params[0], params[1], params[2])
var t float64
fmt.Println("Enter a time (t) to calculate the displacement, with the \n the acceleration (a), initial velocity (v0) and (s0) you executed the program:")
_, _ = fmt.Scan(&t)
fmt.Println("The displacement for t=",t,"s(t):", fn(t))
}
Class
: gathering ofMethods
(functions
) anddata-fields
that operate in the same context.Object
: an instance of aClass
, with data - particulars.
- Has key-word
class
.
class Point:
def __init__(self,xval,yval):
# Assignment, so to start x,y in the class encapsulation
self.x = xval
self.y = yval
The package environment has the same property that the Class
environment in Python.
Data-fields
are called ”receiver-type
”.Methods
receivereceiver-types
in Go.Methods
only operates on types declared on the package’s environment they are in.- Data is passed implicitly to
Methods
.
import "fmt"
type MyInt int
func (mi MyInt) MyMethod () int {
return int(mi + 1)
}
func main (){
var x MyInt
x = 10
fmt.Println(x.MyMethod())
}
For a Method
to receive many Args
, it must be bundled in a Struct
.
Because, a Method
call would follow the structure: Obj.Method()
. Therefore,
it’s necessary that Methods
only receive a unique Object
.
E.g., the methods that use a variety of arguments must be implemented receive the tuple of these arguments.
import (
"fmt"
"math"
)
type Point struct {
x float64
y float64
}
func (p Point) DistToOrig() float64 {
t := math.Pow(p.x,2) + math.Pow(p.y,2)
return math.Sqrt(t)
}
func main () {
p := Point{x: 1.3, y: (-1 * math.Pi)}
fmt.Println(p.DistToOrig())
}
import "fmt"
type MyInt int
func (mi MyInt) MyMethod () int {
return int(mi + 1)
}
func main (){
var x MyInt
x = 10
fmt.Println(x.MyMethod())
}
import "fmt"
type MyIntBR struct {
x float64
y float64
}
type MyIntBV struct {
x float64
y float64
}
func (mi *MyIntBR) MyMethodBR (xx,yy float64) {
mi.x = mi.x + xx + 1
mi.y = mi.y + yy + 2
}
func (mi MyIntBV) MyMethodBV (xx,yy float64) (float64,float64){
return (mi.x + xx + 1), (mi.y + yy + 2)
}
func main (){
fmt.Println("With Call By Reference:")
var z MyIntBR
z = MyIntBR{x: 10, y: 22}
fmt.Println(z.x, z.y)
z.MyMethodBR(10,22)
fmt.Println(z.x, z.y)
fmt.Println("With Call By Value:")
var w MyIntBV
w = MyIntBV{x: 10, y: 22}
w.MyMethodBV(99, 299)
fmt.Println(w.x, w.y)
wx, wy := w.MyMethodBV(10,22)
fmt.Println(wx, wy)
}
package main
import (
"fmt"
"reflect"
)
type Animal struct {
// cow := Animal{Food: "grass", Locomotion: "walk", Noise: "moo"}
Food string
Locomotion string
Noise string
}
type Animals struct {
Animals []Animal
}
func (a Animal) Eat() {
fmt.Println("Food eaten", a.Food)
}
func (a Animal) Walk() {
fmt.Println("Locomotion method:", a.Locomotion)
}
func (a Animal) Speak() {
fmt.Println("Spoken sound:", a.Noise)
}
// func (as Animals) anyAnimal() string {
// }
func main() {
cow := Animal{Food: "grass", Locomotion: "walk", Noise: "moo"}
bird := Animal{Food: "worms", Locomotion: "fly", Noise: "peep"}
snake := Animal{Food: "mice", Locomotion: "slither", Noise: "hsss"}
animals := Animals{Animals: []Animal{cow, bird, snake}}
// var animals []Animal
// animals = make([]Animal, 3)
// animals[0] = cow
// animals[1] = bird
// var animals Animals
// animals = make(animals2, 0, 3)
// var input string
// if input != "X" {
// fmt.Println(">")
// fmt.Scan(&input)
// }
v := reflect.ValueOf(cow)
// V := reflect.ValueOf(animals)
typeOfv := v.Type()
// typeOfV := reflect.ValueOf(animals)
for i := 0; i < v.NumField(); i++ {
fmt.Printf("Field: %s\tValue: %v\n", typeOfv.Field(i).Name, v.Field(i).Interface())
}
fmt.Println(animals)
// for i := 0; i < 3; i++ {
// fmt.Printf("Field: %s\tValue: %v\n", typeOfV.Field(i).Name, V.Field(i).Interface())
// }
}
package main
import (
"fmt"
"strings"
)
type Animal struct {
Food string
Locomotion string
Noise string
}
type Animals struct {
Animals []Animal
}
func (a Animal) Eat() {
fmt.Println("Food eaten", a.Food)
}
func (a Animal) Walk() {
fmt.Println("Locomotion method:", a.Locomotion)
}
func (a Animal) Speak() {
fmt.Println("Spoken sound:", a.Noise)
}
type DoubleStrLists struct {
list1 []string
list2 []string
}
func (lists DoubleStrLists) PossibleStrings(input string) {
for _, string1 := range lists.list1 {
for _, string2 := range lists.list2 {
var concatStr = []string{string1, string2}
if strings.Join(concatStr, " ") == input {
fmt.Println(concatStr[0], concatStr[1])
} else {
fmt.Println("Either the animal-behavior input is incorrect,\n or the animal-behavior is not listed")
}
}
}
}
func main() {
// cow := Animal{Food: "grass", Locomotion: "walk", Noise: "moo"}
// bird := Animal{Food: "worms", Locomotion: "fly", Noise: "peep"}
// snake := Animal{Food: "mice", Locomotion: "slither", Noise: "hsss"}
fmt.Println("Use key-words in lower-case. E.g., cow eat, bird move, snake speak")
var input string
if input != "X" {
fmt.Println(">")
fmt.Scan(&input)
var strings DoubleStrLists
strings = DoubleStrLists{list1: []string{"cow", "bird", "snake"}, list2: []string{"eat", "move", "speak"}}
strings.PossibleStrings(input)
// if strings.PossibleStrings(input) {
// }
}
// for i := 0; i < v.NumField(); i++ {
// fmt.Printf("Field: %s\tValue: %v\n", typeOfv.Field(i).Name, v.Field(i).Interface())
// }
// fmt.Println(animals)
}
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
type Animal struct {
Food string
Locomotion string
Noise string
}
type Animals struct {
Animals []Animal
}
func (a Animal) Eat() {
fmt.Println("Food eaten:", a.Food)
}
func (a Animal) Walk() {
fmt.Println("Locomotion method:", a.Locomotion)
}
func (a Animal) Speak() {
fmt.Println("Spoken sound:", a.Noise)
}
type DoubleStrLists struct {
list1 []string
list2 []string
}
func (lists DoubleStrLists) PossibleStrings(input string) (string, string) {
var animal string
var behavior string
for _, string1 := range lists.list1 {
for _, string2 := range lists.list2 {
var concatStr = []string{string1, string2}
if strings.Join(concatStr, " ") == input {
fmt.Println("It's in the possible list, in which", "animal:", concatStr[0], "behavior:", concatStr[1])
animal, behavior = concatStr[0], concatStr[1]
}
}
}
return animal, behavior
}
// func (lists DoubleStrLists) PossibleStringsPrint() {
// for _, string1 := range lists.list1 {
// for _, string2 := range lists.list2 {
// var concatStr = []string{string1, string2}
// fmt.Println(strings.Join(concatStr, " "))
// }
// }
// }
func main() {
cow := Animal{Food: "grass", Locomotion: "walk", Noise: "moo"}
bird := Animal{Food: "worms", Locomotion: "fly", Noise: "peep"}
snake := Animal{Food: "mice", Locomotion: "slither", Noise: "hsss"}
fmt.Println("Use key-words in lower-case. \nE.g., cow eat, bird move, snake speak\n If you want to exit, press 'X' and Enter")
for {
var input string
fmt.Println(">")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan() // use `for scanner.Scan()` to keep reading
input = scanner.Text()
fmt.Println("captured:", input)
if input == "X" {
break
} else {
var strings DoubleStrLists
strings = DoubleStrLists{list1: []string{"cow", "bird", "snake"}, list2: []string{"eat", "move", "speak"}}
animal, behavior := strings.PossibleStrings(input)
switch animal {
case "cow":
switch behavior {
// Eat Walk Speak
case "eat":
cow.Eat()
case "move":
cow.Walk()
case "speak":
cow.Speak()
}
case "bird":
switch behavior {
// Eat Walk Speak
case "eat":
bird.Eat()
case "move":
bird.Walk()
case "speak":
bird.Speak()
}
case "snake":
switch behavior {
// Eat Walk Speak
case "eat":
snake.Eat()
case "move":
snake.Walk()
case "speak":
snake.Speak()
}
}
}
}
}
The ability to make methods
behave in context. For different Object-types
methods can use different algorithms to compute the same phenomena associated
with the object. E.g., different ways to measure area, depending on the
geometric object.
Abstractions go from particulars to generalizations, for example
dog -> mammal -> animal -> living being.
cannabis sativa -> rosales -> angiosperms -> plant -> living being.
As we can see, very different objects
can have the same underlying high-order
abstraction - e.i., ‘dog’ and ‘cannabis sativa’ being ‘living beings’.
Polymorphism
would be a description, through methods
, that is shared to all
these objects, on a high-order-abstraction context. e.g., “energy-consumption”,
“reproduction”, “nervous-terminals”, “evolution”, “natural selection” etc.
- Subclass (lower-level-abstraction) redefining a method inherited from (higher-level-abstraction) superclass. See Inheritance (not supported on Go).
If it looks like a duck, walks like a duck, swims like a duck and quacks like a duck, it’s a duck.
A
type
satisfies an interface if it defines allmethods
specified in theinterface
.
- Ian Harris, University of California, Irvine; Functions, Methods, and Interfaces in Go
Interfaces contains a set of method signatures
, e.i., name, parameters, return
values.
type Shape2D interface{
Area() float64
Perimeter() float64
}
type Triangle {...}
func (t Triangle) Area() float64 {...}
func (t Triangle) Perimeter() float64 {...}
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
type Animal interface {
Eat ()
Walk ()
Noise ()
}
type Cow struct {
Food string
Locomotion string
Noise string
}
type Bird struct {
Food string
Locomotion string
Noise string
}
type Snake struct {
Food string
Locomotion string
Noise string
}
func (a Cow) Eat() {
fmt.Println("Food eaten:", a.Food)
}
func (a Cow) Walk() {
fmt.Println("Locomotion method:", a.Locomotion)
}
func (a Cow) Speak() {
fmt.Println("Spoken sound:", a.Noise)
}
func (a Bird) Eat() {
fmt.Println("Food eaten:", a.Food)
}
func (a Bird) Walk() {
fmt.Println("Locomotion method:", a.Locomotion)
}
func (a Bird) Speak() {
fmt.Println("Spoken sound:", a.Noise)
}
func (a Snake) Eat() {
fmt.Println("Food eaten:", a.Food)
}
func (a Snake) Walk() {
fmt.Println("Locomotion method:", a.Locomotion)
}
func (a Snake) Speak() {
fmt.Println("Spoken sound:", a.Noise)
}
type DoubleStrLists struct {
list1 []string
list2 []string
}
func (lists DoubleStrLists) PossibleStrings(input string) (string, string) {
var animal string
var behavior string
for _, string1 := range lists.list1 {
for _, string2 := range lists.list2 {
var concatStr = []string{string1, string2}
if strings.Join(concatStr, " ") == input {
fmt.Println("It's in the possible list, in which", "animal:", concatStr[0], "behavior:", concatStr[1])
animal, behavior = concatStr[0], concatStr[1]
}
}
}
return animal, behavior
}
func main() {
cow := Cow{Food: "grass", Locomotion: "walk", Noise: "moo"}
bird := Bird{Food: "worms", Locomotion: "fly", Noise: "peep"}
snake := Snake{Food: "mice", Locomotion: "slither", Noise: "hsss"}
fmt.Println("Use key-words in lower-case. \nE.g., cow eat, bird move, snake speak\n If you want to exit, press 'X' and Enter")
for {
var input string
fmt.Println(">")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan() // use `for scanner.Scan()` to keep reading
input = scanner.Text()
fmt.Println("captured:", input)
if input == "X" {
break
} else {
var s Animal
var strings DoubleStrLists
strings = DoubleStrLists{list1: []string{"cow", "bird", "snake"}, list2: []string{"eat", "move", "speak"}}
animal, behavior := strings.PossibleStrings(input)
switch animal {
case "cow":
switch behavior {
case "eat":
s = cow
s.Eat()
case "move":
s = cow
s.Walk()
case "speak":
s = cow
s.Speak()
}
case "bird":
switch behavior {
case "eat":
s = bird
s.Eat()
case "move":
s = bird
s.Walk()
case "speak":
s = bird
s.Speak()
}
case "snake":
switch behavior {
case "eat":
s = snake
s.Eat()
case "move":
s = snake
s.Walk()
case "speak":
s = snake
s.Speak()
}
}
}
}
}
Two programs executing at the same time.
Given processors P1 and P2, at a given time, both are performing on the same instruction.
Need replicated hardware.
Memory access time is a performance bottleneck.
- Use of internal memory takes 100 cycles to happen.
- Use of cache memory takes 1 cycle.
- Physically restrict.
- Parallel execution is needed to exploit multi-core systems.
- Code made to execute on multiple cores.
- Different programs executing on different cores.
Programmers determine which tasks can be executed in parallel.
Mapping tasks to hardware usually are determined by:
- Operational System.
- Go runtime scheduler.
- Sequential execution.
- Can be run in one hardware.
- Must be run in multiple hardware.
- Sequential and simultaneous run.
- Concurrency improves performance, even without parallelism.
- Tasks must periodically wait for something.
- E.g., access memory.
- Other concurrent tasks can operate while one task is waiting.
What we want is to parallelize two executions of print, in this example
package main
import (
"fmt"
)
func main() {
go fmt.Printf("New routine")
fmt.Printf("Main routine")
}
We see that we weren’t able to make fmt.Printf("New routine")
run parallel to fmt.Printf("Main routine")
. Because, the main
routine finished before the parallel thread is executed.
We can schedule tasks, so we make sure to run everything we want, before the main routine ends.
package main
import (
"fmt"
"sync"
)
func foo(wg *sync.WaitGroup) {
fmt.Printf("New routine\n") // -> used to be in the main function directly
wg.Done()
}
func main() {
// New lines ------
var wg sync.WaitGroup
wg.Add(1)
// ------ **
go foo(&wg) // -> old: go fmt.Printf("New routine")
// ------ **
wg.Wait()
// ------------------
fmt.Printf("Main routine")
}