LoginSignup
1
1

More than 5 years have passed since last update.

golangぷちめも3

Posted at

sync.errgroupとcontext周りの実装見てたら今後もしかしたら何かに使うかもしれない実装方法があったのでメモっておく

package main

import "yourpath/etc"

func main() {
    obj, die := etc.NewEtc()
    obj.Oops()
    die()
}
package etc

import "fmt"

type etc struct {}
type EtcFunc func()

func NewEtc() (*etc, EtcFunc) {
    e := &etc{}
    return e, func() { e.die() }
}

func (e *etc) Oops() {
    fmt.Println("oops.")
}

func (e *etc) die() {
    fmt.Println("call die.")
}

contextのほうだとcancelをこれで実現してたが、なんかほほーってなったので残し

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1