Исходники и презентации

This commit is contained in:
2025-05-23 07:26:39 +03:00
parent aa948179d5
commit 02d8430a3a
514 changed files with 13773 additions and 0 deletions

View File

@ -0,0 +1,25 @@
package main
import (
"log"
"runtime"
)
func infiniteLoop(str string) {
for {
log.Println(str)
}
}
func loop(str string) {
for i := 0; i < 5; i++ {
runtime.Gosched()
log.Println(str)
}
}
func main() {
runtime.GOMAXPROCS(1)
go infiniteLoop("infinite_loop")
loop("loop")
}

View File

@ -0,0 +1,19 @@
package main
import (
"fmt"
"runtime"
)
func main() {
runtime.GOMAXPROCS(1)
var i int
go func() {
for {
i++
}
}()
fmt.Println(i)
}

View File

@ -0,0 +1,16 @@
package main
import (
"fmt"
"runtime"
)
func main() {
fmt.Println("GOMAXPROCS:", runtime.GOMAXPROCS(0))
fmt.Println("CPU:", runtime.NumCPU())
runtime.GOMAXPROCS(16)
fmt.Println("GOMAXPROCS:", runtime.GOMAXPROCS(0))
fmt.Println("CPU:", runtime.NumCPU())
}

View File

@ -0,0 +1,16 @@
package main
import (
"fmt"
"time"
)
func main() {
for i := 0; i < 5; i++ {
go func() {
fmt.Print(i)
}()
}
time.Sleep(2 * time.Second)
}

View File

@ -0,0 +1,10 @@
package main
import (
"fmt"
"runtime"
)
func main() {
fmt.Printf("Goroutines: %d\n", runtime.NumGoroutine())
}

View File

@ -0,0 +1,33 @@
package main
import (
"log"
"time"
)
func task() {
for {
time.Sleep(time.Millisecond * 200)
panic("unexpected situation")
}
}
func NeverExit(name string, action func()) {
defer func() {
if v := recover(); v != nil {
log.Println(name, "is crashed - restarting...")
go NeverExit(name, action)
}
}()
if action != nil {
action()
}
}
func main() {
go NeverExit("first_goroutine", task)
go NeverExit("second_goroutine", task)
time.Sleep(time.Second)
}

View File

@ -0,0 +1,23 @@
package main
import (
"fmt"
"time"
)
func process() {
defer func() {
v := recover()
fmt.Println("recovered:", v)
}()
go func() {
panic("error")
}()
time.Sleep(time.Second)
}
func main() {
process()
}

View File

@ -0,0 +1,19 @@
package main
import (
"fmt"
"runtime"
"time"
)
func main() {
go func() {
for {
time.Sleep(time.Second)
fmt.Println("tick")
}
}()
time.Sleep(3 * time.Second)
runtime.Goexit()
}

View File

@ -0,0 +1,29 @@
package main
import (
"errors"
"log"
"net"
)
// nc 127.0.0.1 12345
func main() {
listener, err := net.Listen("tcp", ":12345")
if err != nil {
log.Fatal(err)
}
for {
conn, err := listener.Accept()
if err != nil {
log.Println(err)
}
go ClientHandler(conn)
}
}
func ClientHandler(c net.Conn) {
panic(errors.New("internal error"))
}

View File

@ -0,0 +1,35 @@
package main
import (
"log"
"net"
)
// nc 127.0.0.1 12345
func main() {
listener, err := net.Listen("tcp", ":12345")
if err != nil {
log.Fatal(err)
}
for {
conn, err := listener.Accept()
if err != nil {
log.Println(err)
}
go ClientHandler(conn)
}
}
func ClientHandler(c net.Conn) {
defer func() {
if v := recover(); v != nil {
log.Println("captured panic:", v)
}
c.Close()
}()
panic("internal error")
}