Archive · Week 5 · Jan 27 – Feb 2, 2025from 3 items
The Go team accepted three proposals that add new capabilities to the runtime trace package and the types package. These changes introduce a flight‑recording API, a variable kind enumeration, and additional type‑and‑value helpers.
Worth knowingruntime
runtime/trace: flight recording
- What changed
- The
tracepackage now includes aFlightRecordertype that can record a moving window of execution trace data. Only one flight recorder may be active at a time, and it can run concurrently with a normal trace consumer. - Production impact
- The source does not say.
- Try it
- Create a
trace.NewFlightRecorderand callStart()to begin recording. - Source
- github.com/golang/go/issues/63185
Explain it and run it
Understand it, then run it
The Go trace package can record a program’s execution as a stream of events. Until now you could start a trace, let it run, and then write the collected data to a file. A new feature, called *flight recording*, adds a FlightRecorder type that keeps a moving window of the most recent trace data in memory. When something interesting happens you can ask the recorder to dump that recent window to a file, without having to restart the program or replay a long trace.
Run it now
// This program demonstrates the current trace API. The new FlightRecorder
// type is not yet available in Go 1.27.1, so we use the standard trace.Start
// and trace.Stop functions instead.
package main
import (
"fmt"
"os"
"time"
"runtime/trace"
)
func main() {
// Open a file to write the trace data.
f, err := os.Create("trace.out")
if err != nil {
fmt.Println("could not create trace file:", err)
return
}
defer f.Close()
// Start tracing. The trace will capture all events until Stop is called.
if err := trace.Start(f); err != nil {
fmt.Println("could not start trace:", err)
return
}
defer trace.Stop()
// Simulate some work.
time.Sleep(2 * time.Second)
// The trace data is written to trace.out when Stop is called.
fmt.Println("trace written to trace.out")
}
What it printed when we ran it on Go 1.27.1
trace written to trace.out
Run sends this program (for Solidity, the contract and its tests) to our own sandbox, where it is compiled and run once, with no network, and what it printed or the test report comes back here. Nothing is kept. Runs are counted per visitor for the day so everyone gets a turn; the details are on the legal page.
Worth knowingstdlib
go/types: add Info.ImplicitConversions mapping
- What changed
- The
TypeAndValuetype now hasAssignedTo()andUntyped()methods.AssignedTo()returns the type of the variable on the left side of an assignment, andUntyped()returns the untyped constant type of an expression. - Production impact
- The source does not say.
- Try it
- Call
AssignedTo()on aTypeAndValuefrom atypes.Infoto see the target type of an assignment. - Source
- github.com/golang/go/issues/70638
Explain it and run it
Understand it, then run it
Run it now
// This program demonstrates the current behavior of go/types in Go 1.27.1.
// It shows that the Type field of a TypeAndValue reports the default
// (typed) type of an expression, even when the expression is an untyped
// constant. The new AssignedTo and Untyped methods are not yet available,
// so we rely on the existing Type field to illustrate the issue.
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"go/types"
)
func main() {
// A simple source with an untyped constant used in an assignment.
src := `package main
var x any = 1 + 2
`
fset := token.NewFileSet()
// Parse the source.
file, err := parser.ParseFile(fset, "example.go", src, 0)
if err != nil {
panic(err)
}
// Prepare type-checking info.
info := &types.Info{
Types: make(map[ast.Expr]types.TypeAndValue),
}
// Type-check the file.
conf := types.Config{Importer: nil}
if _, err := conf.Check("example", fset, []*ast.File{file}, info); err != nil {
panic(err)
}
// Find the expression 1+2 in the AST.
var expr ast.Expr
ast.Inspect(file, func(n ast.Node) bool {
if e, ok := n.(*ast.BinaryExpr); ok && e.Op == token.ADD {
expr = e
return false
}
return true
})
if expr == nil {
panic("expression not found")
}
tv := info.Types[expr]
// tv.Type reports the default type of the expression, which is 'int'.
fmt.Printf("Type of 1+2 (default type): %s\n", tv.Type)
// The untyped constant type is not directly exposed in 1.27.1.
// In future releases, tv.Untyped() would return UntypedInt.
}
What it printed when we ran it on Go 1.27.1
Type of 1+2 (default type): int
Run sends this program (for Solidity, the contract and its tests) to our own sandbox, where it is compiled and run once, with no network, and what it printed or the test report comes back here. Nothing is kept. Runs are counted per visitor for the day so everyone gets a turn; the details are on the legal page.
Exercise
Write a small program that creates a trace.FlightRecorder, starts it, writes a few trace events, stops the recorder, and then prints the number of recorded events.
The 60-second version
Hello, this week the Go team added a new flight‑recording API to the trace package, letting you capture a moving window of execution trace data. They also expanded the types package with a new Var.Kind method that tells you whether a variable is a package, local, receiver, parameter, result, or field, plus a setter for importers. Finally, they added two helpers to TypeAndValue: AssignedTo, which reveals the target type of an assignment, and Untyped, which exposes the untyped constant type of an expression. These changes give developers more introspection into program structure and execution, but the Go team notes that the source does not say how this will affect production systems.
Written by gpt-oss-20b · claims checked against the sources · archive, not individually reviewed