-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c013463
commit 015ffa2
Showing
63 changed files
with
676,690 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module example/learn-go-in-y-minutes | ||
|
||
go 1.20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github/vanduc1102/read-big-file | ||
|
||
go 1.20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.