Skip to content

Commit

Permalink
more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
vanduc1102 committed Aug 27, 2023
1 parent c013463 commit 015ffa2
Show file tree
Hide file tree
Showing 63 changed files with 676,690 additions and 0 deletions.
1 change: 1 addition & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"recommendations": [
"streetsidesoftware.code-spell-checker",
"esbenp.prettier-vscode",
"humao.rest-client",
"firsttris.vscode-jest-runner",
"dbaeumer.vscode-eslint",
"EditorConfig.EditorConfig",
Expand Down
3 changes: 3 additions & 0 deletions go-lang/digital-ocean-multifunc/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github/vanduc1102/digital-ocean-multifunc

go 1.20
Empty file.
42 changes: 42 additions & 0 deletions go-lang/digital-ocean-multifunc/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"fmt"
"sync"
"time"
)

func generateNumbers(total int, wg *sync.WaitGroup, ch chan<- int) {
defer wg.Done()
for i := 1; i <= total; i++ {
fmt.Printf("sending %d to channel time=%v\n", i, time.Now())
time.Sleep(time.Second * 2)
ch <- i
}
}

func printNumbers(wg *sync.WaitGroup, ch <-chan int) {
defer wg.Done()
for value := range ch {
time.Sleep(time.Second)
fmt.Printf("reading %d from channel time=%v\n", value, time.Now())
}
}

func main() {
var wg sync.WaitGroup

numberChannel := make(chan int)
for i := 0; i < 12; i++ {
wg.Add(1)
go printNumbers(&wg, numberChannel)
}

generateNumbers(3, &wg, numberChannel)

close(numberChannel)

fmt.Printf("Waiting for goroutines to finish... time=%v\n", time.Now())
wg.Wait()
fmt.Printf("Done at time=%v\n", time.Now())
}
10 changes: 10 additions & 0 deletions go-lang/hello/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module example/hello

go 1.20

require rsc.io/quote v1.5.2

require (
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c // indirect
rsc.io/sampler v1.3.0 // indirect
)
6 changes: 6 additions & 0 deletions go-lang/hello/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:qgOY6WgZOaTkIIMiVjBQcw93ERBE4m30iBm00nkL0i8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
rsc.io/quote v1.5.2 h1:w5fcysjrx7yqtD/aO+QwRjYZOKnaM9Uh2b40tElTs3Y=
rsc.io/quote v1.5.2/go.mod h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0=
rsc.io/sampler v1.3.0 h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
13 changes: 13 additions & 0 deletions go-lang/hello/hello.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// https://go.dev/doc/tutorial/getting-started

package main

import (
"fmt"

"rsc.io/quote"
)

func main() {
fmt.Println("Hello, World: ", quote.Go())
}
3 changes: 3 additions & 0 deletions go-lang/learn-go-in-y-minutes/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module example/learn-go-in-y-minutes

go 1.20
94 changes: 94 additions & 0 deletions go-lang/learn-go-in-y-minutes/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// https://learnxinyminutes.com/docs/go/

package main

import (
"fmt"
"os"
"time"
)

func main() {
fmt.Println("Hello , World! from Learn GoLang in Y minutes")
fmt.Println("The time is ", time.Now())
beyondHello()
}

func beyondHello() {
var x int
x = 3
y := 4

sum, prod := learnMultiple(x, y)
fmt.Println("sum: ", sum, "prod: ", prod)
learnTypes()
}

func learnMultiple(x, y int) (sum, prod int) {
return x + y, x * y
}

func learnTypes() {
str := "Learn Go!"

s2 := `A \"raw\" string literal
can include line breaks.`

g := 'Σ'

f := 3.1419
c := 3 + 4i

var u uint = 7
var pi float32 = 22. / 7

n := byte('\n')

var a4 [4]int
a5 := [...]int{3, 1, 5, 10, 100}

a4_cpy := a4
a4_cpy[0] = 25

fmt.Println(a4_cpy[0] == a4[0])

s3 := []int{4, 5, 9}
s4 := make([]int, 4)
var d2 [][]float64
bs := []byte("a slide")

s3_cpy := s3
s3_cpy[0] = 0
fmt.Println(s3_cpy[0] == s3[0])

s := []int{1, 2, 3}
s = append(s, 4, 5, 6)
fmt.Println(s)

s = append(s, []int{7, 8, 9}...)
fmt.Println(s)

p, q := learnMemory()
fmt.Println(*p, *q)

m := map[string]int{"three": 3, "four": 4}
m["one"] = 1

_, _, _, _, _, _, _, _, _, _, _, _, _ = str, s2, g, f, c, pi, n, a5, a4, s4, d2, bs, u

file, _ := os.Create("output.txt")
fmt.Fprint(file, "This is how you write to a file, by the way")
file.Close()

fmt.Println(s, c, a4, s3, d2, m)
}

func learnMemory() (p, q *int) {
p = new(int)

s := make([]int, 20)
s[3] = 7
r := -2
return &s[3], &r
}
1 change: 1 addition & 0 deletions go-lang/learn-go-in-y-minutes/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is how you write to a file, by the way
Empty file.
3 changes: 3 additions & 0 deletions go-lang/read-big-file/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github/vanduc1102/read-big-file

go 1.20
135 changes: 135 additions & 0 deletions go-lang/read-big-file/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package main

import (
"encoding/csv"
"fmt"
"io"
"log"
"os"
"strconv"
"sync"
"time"
)

type MovieLink struct {
Id int
ImdbId string
TmdbId string
}

const (
Odd string = "odd"
Even string = "even"
)

func main() {
var wg sync.WaitGroup

startTime := time.Now()
file, err := os.Open("ml-latest/links.csv")
if err != nil {
fmt.Println(err)
return
}
defer file.Close()

items := 10000
itemsPerWorker := 100
kind := Odd
numberWorkers := 1
if items > itemsPerWorker {
numberWorkers := items / itemsPerWorker
if items%itemsPerWorker != 0 {
numberWorkers++
}
}

jobQueue := make(chan []string, numberWorkers)

wg.Add(1)
go func() {
csvReader := csv.NewReader(file)

if _, err := csvReader.Read(); err != nil {
log.Fatal(err)
}

for {
row, err := csvReader.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
jobQueue <- row
}
defer wg.Done()
}()

outputChannel := make(chan []MovieLink, numberWorkers)
for i := 0; i < numberWorkers; i++ {
wg.Add(1)
go func() {
worker(jobQueue, outputChannel, itemsPerWorker, kind)
wg.Done()
}()
}

movies := make([]MovieLink, items)
for chunk := range outputChannel {
fmt.Printf("== lines : %d\n", len(movies))
movies = append(movies, chunk...)
if len(movies) >= items {
close(outputChannel)
break
}
}

// wg.Wait()
fmt.Printf("lines : %d\n", len(movies))
fmt.Printf("The seconds difference is: %fs\n", time.Since(startTime).Seconds())
}

func worker(jobChannel <-chan []string, output chan<- []MovieLink, itemsPerWorker int, kind string) {
movies := make([]MovieLink, itemsPerWorker)
for line := range jobChannel {
item, err := marshal(line)
if err == nil {
if kind == Odd && item.Id%2 == 1 {
// fmt.Printf("Add odd %s\n", line)
movies = append(movies, *item)
}
if kind == Even && item.Id%2 == 0 {
// fmt.Printf("Add even %s\n", line)
movies = append(movies, *item)
}
fmt.Printf("collected size %d\n", len(movies))
if len(movies) == itemsPerWorker {
output <- movies
break
}
} else {
log.Fatal(err)
}
}
}

func marshal(row []string) (*MovieLink, error) {
var movie MovieLink
for j, field := range row {
if j == 0 {
id, err := strconv.Atoi(field)
if err != nil {
return nil, err
}
movie.Id = id
} else if j == 1 {
movie.ImdbId = field
} else if j == 2 {
movie.TmdbId = field
}
}

return &movie, nil
}
Loading

0 comments on commit 015ffa2

Please sign in to comment.