Skip to content

Instantly share code, notes, and snippets.

@obutora
obutora / main.go
Last active May 27, 2024 07:52
Collyを使ったCrawler
package main
import (
"fmt"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/proto"
"github.com/gocolly/colly/v2"
)
@obutora
obutora / main.go
Created May 21, 2024 13:02
goroutineを使ったエラーハンドリング
package main
import (
"fmt"
"net/http"
)
// errorとレスポンスをセットにしてラップする構造体
type Result struct {
Error error
@obutora
obutora / main.go
Created May 21, 2024 12:17
for-selectパターン
package main
import "fmt"
// <- chan int はint型の受信専用チャネル
// この関数は、intを受信する専用のチャネルを返す
func generate(done chan struct{}) <-chan int {
result := make(chan int)
go func() {
defer close(result)
@obutora
obutora / mainl.go
Last active May 20, 2024 13:10
channelを閉じることでgoroutineをトリガする
package main
import (
"fmt"
"sync"
)
func main() {
begin := make(chan bool, 10)
var wg sync.WaitGroup
@obutora
obutora / memory.go
Created May 18, 2024 11:17
goでメモリ使用量を調べる
func main() {
mem := func () uint64 {
runtime.GC()
var s runtime.MemStats
runtime.ReadMemStats(&s)
// Sys 以外にも [Alloc, TotalAlloc, Mallocs, Frees, HeapAlloc, HeapSys, HeapIdle, HeapInuse, HeapReleased, HeapObjects, StackInuse, StackSys, MSpanInuse, MSpanSys, MCacheInuse, MCacheSys, BuckHashSys, GCSys, OtherSys] などがある
return s.Sys
}
package main
import (
"fmt"
"time"
)
func main() {
jst, _ := time.LoadLocation("Asia/Tokyo")