diff --git a/headFirst/flag/flag b/headFirst/flag/flag new file mode 100755 index 0000000..9fa8418 Binary files /dev/null and b/headFirst/flag/flag differ diff --git a/headFirst/flag/go.mod b/headFirst/flag/go.mod new file mode 100644 index 0000000..7012a64 --- /dev/null +++ b/headFirst/flag/go.mod @@ -0,0 +1,3 @@ +module main + +go 1.25.3 diff --git a/headFirst/flag/main.go b/headFirst/flag/main.go new file mode 100644 index 0000000..281780a --- /dev/null +++ b/headFirst/flag/main.go @@ -0,0 +1,15 @@ +package main + +import ( + "flag" + "fmt" +) + +func main() { + var nFlag = flag.Int("n",1234,"this is test int flag") + var sFlag = flag.String("s","lolkek","this is test string flag") + flag.Parse() + + fmt.Println(*nFlag) + fmt.Println(*sFlag) +} diff --git a/headFirst/generics/main.go b/headFirst/generics/main.go index 38eb1f0..b8f2aa8 100644 --- a/headFirst/generics/main.go +++ b/headFirst/generics/main.go @@ -63,5 +63,7 @@ func main() { var e Test _ = json.Unmarshal(b,&e) - fmt.Println(e) + fmt.Printf("Output is %v",e) + + } diff --git a/headFirst/routines/main.go b/headFirst/routines/main.go index e630597..367b2e5 100644 --- a/headFirst/routines/main.go +++ b/headFirst/routines/main.go @@ -2,45 +2,50 @@ package main import ( "fmt" - "io" - "log" - "net/http" + "sync" + // "time" ) -func errCheck(err error) { - if err != nil { - log.Fatalln("Error happened %v\n",err) + +// func a(name int, channel chan int, wg *sync.WaitGroup) { +// defer wg.Done() +// for i := 0; i < 3; i++ { +// fmt.Printf("Number %d is sending\n",name) +// channel <- name +// } +// fmt.Printf("Num %d is ended\n",name) +// } +func test(name int,channel chan []int, wg *sync.WaitGroup) { + defer wg.Done() + inter := []int{} + for i := 0; i < 3; i++ { + inter = append(inter, name) } + channel <- inter } -func responseSize(url string, out chan Page) { - fmt.Println("Getting ",url) - response,err := http.Get(url) - errCheck(err) - defer response.Body.Close() - body,err := io.ReadAll(response.Body) - errCheck(err) - - out <- Page{URL: url, Size: len(body)} - -} - -type Page struct { - URL string - Size int -} func main() { - size := make(chan Page) - urls :=[]string{"https://google.com","https://golang.org","https://golang.org/doc"} - for _,url := range urls { - go responseSize(url,size) + + var wg sync.WaitGroup + _list := []int{1,2,3,4,5} + a_chan := make(chan []int) + + for _,k := range _list { + wg.Add(1) + go test(k,a_chan, &wg) } - fmt.Println("--------") - for i:=0; i < len(urls);i++ { - page := <- size - fmt.Printf("%s: %d\n",page.URL,page.Size) + + go func() { + wg.Wait() + close(a_chan) + }() + + // for i := range a_chan { + // fmt.Println(i) + // } + for i := range a_chan { + fmt.Println(i) } - fmt.Println("End main()") }