package main
import (
"context"
"errors"
"fmt"
"io"
"sync"
"time"
"unsafe"
)
type InnerStruct struct{}
type PublicInnerStruct = InnerStruct
const MaxSize = 1024
const DefaultName = "default"
var (
counter uint32
instance sync.Once
config *Config
)
type Result[T any] struct {
Value T
Err error
}
type NodeID uint32
type SharedData *[]byte
type Handler[T any] func(T) (T, error)
type Config struct {
Name string `json:"name"`
port uint16 `json:"port"`
Enabled bool `json:"enabled" deprecated:"true"`
phantom struct{} }
type Point struct {
X, Y, Z float64
}
type Marker struct{}
type BorrowedData struct {
data string
mutable []byte
ctx context.Context }
type Status int
const (
StatusActive Status = iota
StatusInactive
StatusPending
StatusComplex
)
type StatusData interface {
isStatus()
}
type StatusActive struct{}
type StatusInactive struct{ Reason string }
type StatusPending struct{ Duration time.Duration }
type StatusComplex struct {
ID uint32
Data []byte
}
func (StatusActive) isStatus() {}
func (StatusInactive) isStatus() {}
func (StatusPending) isStatus() {}
func (StatusComplex) isStatus() {}
type Option[T any] interface {
isOption()
}
type Some[T any] struct{ Value T }
type None[T any] struct{}
func (Some[T]) isOption() {}
func (None[T]) isOption() {}
type Parser[Input, Output any] interface {
Parse(input Input) (Output, error)
Validate(input Input) bool
New() Parser[Input, Output] }
type Container[T any] interface {
Add(item T)
Get(index int) *T
Iter() <-chan T }
type Lifecycle[T any] interface {
Process(ctx context.Context) T
}
const MaxDepth = 100
const DefaultPort uint16 = 8080
func NewConfig(name string) *Config {
return &Config{
Name: name,
port: DefaultPort,
Enabled: true,
phantom: struct{}{},
}
}
func (c *Config) Port() uint16 {
return c.port
}
func (c *Config) SetPort(port uint16) {
c.port = port
}
func (c Config) IntoName() string {
return c.Name
}
func WithData[T any](c *Config, data T) (*Config, T) {
newConfig := *c
return &newConfig, data
}
func (c *Config) Connect(ctx context.Context) error {
go func() {
time.Sleep(100 * time.Millisecond)
}()
return nil
}
func (c *Config) GetRawPtr() unsafe.Pointer {
return unsafe.Pointer(&c.port)
}
type ConfigParser struct{}
func (cp ConfigParser) Parse(input string) (*Config, error) {
return NewConfig(input), nil
}
func (cp ConfigParser) Validate(input string) bool {
return true
}
func (cp ConfigParser) New() Parser[string, *Config] {
return ConfigParser{}
}
type GenericContainer[T any, U comparable] struct {
items []T
metadata U
}
func NewGenericContainer[T any, U comparable]() *GenericContainer[T, U] {
var zero U
return &GenericContainer[T, U]{
items: make([]T, 0),
metadata: zero,
}
}
func (gc *GenericContainer[T, U]) Add(item T) {
gc.items = append(gc.items, item)
}
func (gc *GenericContainer[T, U]) Get(index int) *T {
if index < len(gc.items) {
return &gc.items[index]
}
return nil
}
func (gc *GenericContainer[T, U]) Iter() <-chan T {
ch := make(chan T)
go func() {
defer close(ch)
for _, item := range gc.items {
ch <- item
}
}()
return ch
}
func ComplexFunction[T any, U fmt.Stringer](
reference string,
mutable *[]T,
owned string,
generic U,
closure func() T,
) (string, error) {
*mutable = append(*mutable, closure())
return reference, nil
}
func AsyncOperation(ctx context.Context, url string) <-chan Result[string] {
result := make(chan Result[string], 1)
go func() {
defer close(result)
time.Sleep(10 * time.Millisecond)
result <- Result[string]{Value: url, Err: nil}
}()
return result
}
func ConstFunction(x uint32) uint32 {
return x * 2
}
func UnsafeOperation(ptr unsafe.Pointer) {
*(*byte)(ptr) = 0
}
func ReturnsInterface() fmt.Stringer {
return &stringWrapper{"hello"}
}
type stringWrapper struct{ s string }
func (sw *stringWrapper) String() string { return sw.s }
func TakesDynInterface(parser Parser[string, *Config]) {
}
func HigherRanked[F ~func(string) string](f F) {
f("test")
}
func GeneratedFunc() {
fmt.Printf("Function: %s\n", "GeneratedFunc")
}
type MyUnion interface {
isUnion()
}
type UnionF1 struct{ Value uint32 }
type UnionF2 struct{ Value float32 }
func (UnionF1) isUnion() {}
func (UnionF2) isUnion() {}
import "C"
func CallExternalFunction(x int32) int32 {
return int32(C.external_function(C.int(x)))
}
type CustomError struct {
Message string
}
func (e *CustomError) Error() string {
return e.Message
}
func TestConfig() error {
config := NewConfig("test")
if config.Port() != DefaultPort {
return fmt.Errorf("expected port %d, got %d", DefaultPort, config.Port())
}
return nil
}
func BenchmarkCreate(b interface{
ResetTimer()
StartTimer()
StopTimer()
}) {
b.ResetTimer()
for i := 0; i < 1000; i++ {
NewConfig("bench")
}
}
func main() {
config := NewConfig("app")
fmt.Printf("Config: %+v\n", config)
fmt.Println("Testing Go features:")
container := NewGenericContainer[int, string]()
container.Add(42)
for item := range container.Iter() {
fmt.Printf("Item: %d\n", item)
break }
ctx := context.Background()
resultChan := AsyncOperation(ctx, "https://example.com")
result := <-resultChan
if result.Err == nil {
fmt.Printf("Async result: %s\n", result.Value)
}
stringer := ReturnsInterface()
fmt.Printf("Interface result: %s\n", stringer.String())
if err := TestConfig(); err != nil {
fmt.Printf("Test failed: %v\n", err)
} else {
fmt.Println("All tests passed!")
}
}