Source file test/fixedbugs/issue16037_run.go

     1  // run
     2  
     3  //go:build !nacl && !js && !wasip1 && !android && !gccgo
     4  
     5  // Copyright 2016 The Go Authors. All rights reserved.
     6  // Use of this source code is governed by a BSD-style
     7  // license that can be found in the LICENSE file.
     8  
     9  package main
    10  
    11  import (
    12  	"bytes"
    13  	"fmt"
    14  	"html/template"
    15  	"io/ioutil"
    16  	"log"
    17  	"os"
    18  	"os/exec"
    19  	"path/filepath"
    20  )
    21  
    22  var tmpl = template.Must(template.New("main").Parse(`
    23  package main
    24  
    25  type T struct {
    26      {{range .Names}}
    27  	{{.Name}} *string
    28  	{{end}}
    29  }
    30  
    31  {{range .Names}}
    32  func (t *T) Get{{.Name}}() string {
    33  	if t.{{.Name}} == nil {
    34  		return ""
    35  	}
    36  	return *t.{{.Name}}
    37  }
    38  {{end}}
    39  
    40  func main() {}
    41  `))
    42  
    43  func main() {
    44  	const n = 5000
    45  
    46  	type Name struct{ Name string }
    47  	var t struct{ Names []Name }
    48  	for i := 0; i < n; i++ {
    49  		t.Names = append(t.Names, Name{Name: fmt.Sprintf("H%06X", i)})
    50  	}
    51  
    52  	buf := new(bytes.Buffer)
    53  	if err := tmpl.Execute(buf, t); err != nil {
    54  		log.Fatal(err)
    55  	}
    56  
    57  	dir, err := ioutil.TempDir("", "issue16037-")
    58  	if err != nil {
    59  		log.Fatal(err)
    60  	}
    61  	defer os.RemoveAll(dir)
    62  	path := filepath.Join(dir, "ridiculous_number_of_fields.go")
    63  	if err := ioutil.WriteFile(path, buf.Bytes(), 0664); err != nil {
    64  		log.Fatal(err)
    65  	}
    66  
    67  	out, err := exec.Command("go", "build", "-o="+filepath.Join(dir, "out"), path).CombinedOutput()
    68  	if err != nil {
    69  		log.Fatalf("build failed: %v\n%s", err, out)
    70  	}
    71  }
    72  

View as plain text