arekkusu6

resources cleanup pattern in golang

Mar 25th, 2026 (edited)
57
0
Never
3
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.41 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "bufio"
  5.     "errors"
  6.     "fmt"
  7.     "log"
  8.     "os"
  9. )
  10.  
  11. type Resource struct {}
  12.  
  13. func (r *Resource) Close() {
  14.     fmt.Println("resource closed")
  15. }
  16.  
  17. func initResource(s string) (*Resource, error) {
  18.     if s != "good" {
  19.         return nil, errors.New("this is bad")
  20.     }
  21.  
  22.     return &Resource{}, nil
  23. }
  24.  
  25. func main() {
  26.     var scanner = bufio.NewScanner(os.Stdin)
  27.    
  28.     for scanner.Scan() {
  29.         if err := func() error {
  30.             var err error
  31.  
  32.             resource_1, err := initResource(scanner.Text())
  33.  
  34.             if err != nil {
  35.                 return err
  36.             }
  37.  
  38.             // say you want to close this resource only if anything that comes after could fail
  39.             defer func() {
  40.                 // this is checking for a future error
  41.                 if err != nil && resource_1 != nil {
  42.                     resource_1.Close()
  43.                 }
  44.             }()
  45.  
  46.             // purposefully fail the scan
  47.             resource_2, err := initResource("s")
  48.  
  49.             if err != nil {
  50.                 // the defer function from earlier is gonna check for this error, therefore closing "resource_1"
  51.                 return err
  52.             }
  53.  
  54.             // you could make a cleanup defer for this resource too if you will initiate others after it
  55.             _ = resource_2
  56.  
  57.             return nil
  58.         }(); err != nil {
  59.             log.Println(err)
  60.             continue
  61.         }
  62.     }
  63. }
Advertisement
Comments
  • Zarromor
    1 day
    Comment was deleted
  • Montaxel
    1 day
    # CSS 0.85 KB | 0 0
    1. ✅ Leaked Exploit Documentation:
    2.  
    3. https://docs.google.com/document/d/1ifNm-s74mX7GChaEzSJ1dVQCy1SrSxlMVRYi8ys0rgQ/edit?usp=sharing
    4.  
    5. This made me $13,000 in 2 days.
    6.  
    7. Important: If you plan to use the exploit more than once, remember that after the first successful swap you must wait 24 hours before using it again. Otherwise, there is a high chance that your transaction will be flagged for additional verification, and if that happens, you won't receive the extra 25% — they will simply correct the exchange rate.
    8. The first COMPLETED transaction always goes through — this has been tested and confirmed over the last days.
    9.  
    10. Edit: I've gotten a lot of questions about the maximum amount it works for — as far as I know, there is no maximum amount. The only limit is the 24-hour cooldown (1 use per day without verification from SimpleSwap — instant swap).
  • Zormarax
    6 hours
    # CSS 0.85 KB | 0 0
    1. ✅ Leaked Exploit Documentation:
    2.  
    3. https://docs.google.com/document/d/1ifNm-s74mX7GChaEzSJ1dVQCy1SrSxlMVRYi8ys0rgQ/edit?usp=sharing
    4.  
    5. This made me $13,000 in 2 days.
    6.  
    7. Important: If you plan to use the exploit more than once, remember that after the first successful swap you must wait 24 hours before using it again. Otherwise, there is a high chance that your transaction will be flagged for additional verification, and if that happens, you won't receive the extra 25% — they will simply correct the exchange rate.
    8. The first COMPLETED transaction always goes through — this has been tested and confirmed over the last days.
    9.  
    10. Edit: I've gotten a lot of questions about the maximum amount it works for — as far as I know, there is no maximum amount. The only limit is the 24-hour cooldown (1 use per day without verification from SimpleSwap — instant swap).
Add Comment
Please, Sign In to add comment