Last active
March 18, 2025 06:55
-
-
Save wjkoh/8a3adea949cd8edddf6975bc8463b420 to your computer and use it in GitHub Desktop.
Go: Data Race Quiz! Shadowing or Stomping?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"io" | |
"time" | |
) | |
type uploader struct{} | |
// Shadowing or Stomping? | |
// TODO: In this function, which lines cause a data race, specifically a simultaneous read and write to the same data? | |
func (u *uploader) BackupAndUpload(ctx context.Context) (string, error) { | |
pr, pw := io.Pipe() | |
go func() { | |
_, err := u.Backup(ctx, pw) | |
pw.CloseWithError(err) | |
}() | |
ctx, cancel := context.WithTimeout(ctx, 10*time.Minute) | |
defer cancel() | |
return u.Upload(ctx, pr) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hint: Line 15,
_, err := u.Backup(ctx, pw)
, potentially reads from X, while line 18,ctx, cancel := context.WithTimeout(ctx, 10*time.Minute)
, writes to X at the same time.