Radar · Go · Archive · Week 8 · Feb 17 – 23, 2025
Remove notion of core types
Worth knowinglanguage
- What changed
- The proposal removes the notion of “core types” from the language specification, replacing the investigative issue with a concrete proposal.
- Production impact
- The source does not say.
- Try it
- Read the proposal text in the issue to see how type constraints are now described.
- Source
- github.com/golang/go/issues/70128
Understand it, then run it
Run it now
// This program demonstrates the current limitation that a slice expression
// cannot be applied directly to a type parameter. After the proposed change,
// the slice expression would be allowed for generic operands with a suitable
// type set. Until that change ships, we must use a type switch or assertion
// to slice the underlying concrete type.
package main
import (
"fmt"
)
// Constraint allows either a slice of int or a string.
type Constraint interface {
~[]int | ~string
}
// sliceGeneric attempts to slice a value of type T.
// Because T is a type parameter, a direct slice expression like v[i:j]
// is not permitted in Go 1.27.1. We work around this by using a type switch.
func sliceGeneric[T Constraint](v T, i, j int) interface{} {
switch x := any(v).(type) {
case []int:
return x[i:j]
case string:
return x[i:j]
default:
return nil
}
}
func main() {
// Example with a slice of int.
intSlice := []int{10, 20, 30, 40, 50}
fmt.Println("Slicing []int:", sliceGeneric(intSlice, 1, 4))
// Example with a string.
str := "hello world"
fmt.Println("Slicing string:", sliceGeneric(str, 6, 11))
}
What it printed when we ran it on Go 1.27.1
Slicing []int: [20 30 40] Slicing string: world
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.
Written by gpt-oss-20b from the linked source · claims checked against the sources · archive, not individually reviewed