Skip to content

Commit a87597d

Browse files
committed
cmd/go/internal: use the newly added strings.CutSuffix method to optimize the code
Signed-off-by: cui fliter <[email protected]>
1 parent c2ede92 commit a87597d

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

src/cmd/go/internal/clean/clean.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,16 +340,14 @@ func clean(p *load.Package) {
340340
continue
341341
}
342342

343-
if strings.HasSuffix(name, "_test.go") {
344-
base := name[:len(name)-len("_test.go")]
343+
if base, found := strings.CutSuffix(name, "_test.go"); found {
345344
allRemove = append(allRemove, base+".test", base+".test.exe")
346345
}
347346

348-
if strings.HasSuffix(name, ".go") {
347+
if base, found := strings.CutSuffix(name, ".go"); found {
349348
// TODO(adg,rsc): check that this .go file is actually
350349
// in "package main", and therefore capable of building
351350
// to an executable file.
352-
base := name[:len(name)-len(".go")]
353351
allRemove = append(allRemove, base, base+".exe")
354352
}
355353
}

src/cmd/go/internal/load/pkg.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3115,10 +3115,10 @@ func PackagesAndErrorsOutsideModule(ctx context.Context, opts PackageOpts, args
31153115
}
31163116
patterns := make([]string, len(args))
31173117
for i, arg := range args {
3118-
if !strings.HasSuffix(arg, "@"+version) {
3118+
p, found := strings.CutSuffix(arg, "@"+version)
3119+
if !found {
31193120
return nil, fmt.Errorf("%s: all arguments must refer to packages in the same module at the same version (@%s)", arg, version)
31203121
}
3121-
p := arg[:len(arg)-len(version)-1]
31223122
switch {
31233123
case build.IsLocalImport(p):
31243124
return nil, fmt.Errorf("%s: argument must be a package path, not a relative path", arg)

0 commit comments

Comments
 (0)