src/cmd/compile/internal/loopvar/testdata/for_complicated_esc_address.go GO 116 lines View on github.com → Search inside
1// Copyright 2023 The Go Authors. All rights reserved.2// Use of this source code is governed by a BSD-style3// license that can be found in the LICENSE file.45package main67import (8	"fmt"9	"os"10)1112func main() {13	ss, sa := shared(23)14	ps, pa := private(23)15	es, ea := experiment(23)1617	fmt.Printf("shared s, a; private, s, a; experiment s, a = %d, %d;  %d, %d;  %d, %d\n", ss, sa, ps, pa, es, ea)1819	if ss != ps || ss != es || ea != pa || sa == pa {20		os.Exit(11)21	} else {22		fmt.Println("PASS")23	}24}2526func experiment(x int) (int, int) {27	sum := 028	var is []*int29	for i := x; i != 1; i = i / 2 {30		for j := 0; j < 10; j++ {31			if i == j { // 10 skips32				continue33			}34			sum++35		}36		i = i*3 + 137		if i&1 == 0 {38			is = append(is, &i)39			for i&2 == 0 {40				i = i >> 141			}42		} else {43			i = i + i44		}45	}4647	asum := 048	for _, pi := range is {49		asum += *pi50	}5152	return sum, asum53}5455func private(x int) (int, int) {56	sum := 057	var is []*int58	I := x59	for ; I != 1; I = I / 2 {60		i := I61		for j := 0; j < 10; j++ {62			if i == j { // 10 skips63				I = i64				continue65			}66			sum++67		}68		i = i*3 + 169		if i&1 == 0 {70			is = append(is, &i)71			for i&2 == 0 {72				i = i >> 173			}74		} else {75			i = i + i76		}77		I = i78	}7980	asum := 081	for _, pi := range is {82		asum += *pi83	}8485	return sum, asum86}8788func shared(x int) (int, int) {89	sum := 090	var is []*int91	i := x92	for ; i != 1; i = i / 2 {93		for j := 0; j < 10; j++ {94			if i == j { // 10 skips95				continue96			}97			sum++98		}99		i = i*3 + 1100		if i&1 == 0 {101			is = append(is, &i)102			for i&2 == 0 {103				i = i >> 1104			}105		} else {106			i = i + i107		}108	}109110	asum := 0111	for _, pi := range is {112		asum += *pi113	}114	return sum, asum115}

Code quality findings 5

Formatted output to console; prefer structured logging for consistency
info correctness fmt-printf
fmt.Printf("shared s, a; private, s, a; experiment s, a = %d, %d; %d, %d; %d, %d\n", ss, sa, ps, pa, es, ea)
Unstructured output; use a structured logging library (e.g., slog, zap, zerolog, logrus)
info correctness fmt-println
fmt.Println("PASS")
Multiple appends without pre-allocation; use make() with capacity when size is known
info performance append-without-prealloc
is = append(is, &i)
Multiple appends without pre-allocation; use make() with capacity when size is known
info performance append-without-prealloc
is = append(is, &i)
Multiple appends without pre-allocation; use make() with capacity when size is known
info performance append-without-prealloc
is = append(is, &i)

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.