File size: 3,211 Bytes
93d826e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//go:build ignore

package main

import (
	"context"
	"fmt"
	"log"
	"sync"
	"time"
)

// Interface definition
type DataProcessor interface {
	Process(ctx context.Context, data interface{}) error
	Validate(data interface{}) bool
}

// Custom error type
type ValidationError struct {
	Field   string
	Message string
}

func (e *ValidationError) Error() string {
	return fmt.Sprintf("validation error on field %s: %s", e.Field, e.Message)
}

// Struct with embedded type and tags
type User struct {
	sync.Mutex
	ID        int64     `json:"id"`
	Name      string    `json:"name"`
	Email     string    `json:"email"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at,omitempty"`
}

// Type alias and constants
type UserID = int64

const (
	MaxRetries   = 3
	DefaultLimit = 100
)

// single line const
const singleLineConst string = "single line const"

// Global variables
var (
	defaultTimeout = time.Second * 30
	processor      DataProcessor
)

// single line var
var singleLineVar string = "single line var"

// Generic type
type Result[T any] struct {
	Data    T
	Error   error
	Retries int
}

// Implementation of DataProcessor
type UserProcessor struct {
	cache map[UserID]*User
	mu    sync.RWMutex
}

func NewUserProcessor() *UserProcessor {
	return &UserProcessor{
		cache: make(map[UserID]*User),
	}
}

func (p *UserProcessor) Process(ctx context.Context, data interface{}) error {
	user, ok := data.(*User)
	if !ok {
		return fmt.Errorf("invalid data type: expected *User")
	}

	p.mu.Lock()
	defer p.mu.Unlock()

	p.cache[user.ID] = user
	return nil
}

func (p *UserProcessor) Validate(data interface{}) bool {
	user, ok := data.(*User)
	return ok && user.Name != "" && user.Email != ""
}

// Channel operations
func processUsers(ctx context.Context, users <-chan *User) <-chan *Result[*User] {
	results := make(chan *Result[*User])

	go func() {
		defer close(results)
		for user := range users {
			select {
			case <-ctx.Done():
				return
			case results <- &Result[*User]{Data: user}:
			}
		}
	}()

	return results
}

// Function with multiple return values and named returns
func createUser(name, email string) (user *User, err error) {
	defer func() {
		if r := recover(); r != nil {
			err = fmt.Errorf("panic recovered: %v", r)
		}
	}()

	user = &User{
		Name:      name,
		Email:     email,
		CreatedAt: time.Now(),
	}

	if !processor.Validate(user) {
		return nil, &ValidationError{Field: "user", Message: "invalid user data"}
	}

	return user, nil
}

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
	defer cancel()

	processor = NewUserProcessor()

	users := make(chan *User)
	results := processUsers(ctx, users)

	// Anonymous struct
	config := struct {
		Workers int
		Buffer  int
	}{
		Workers: 3,
		Buffer:  10,
	}

	var wg sync.WaitGroup
	wg.Add(config.Workers)

	for i := 0; i < config.Workers; i++ {
		go func(id int) {
			defer wg.Done()
			for result := range results {
				if result.Error != nil {
					log.Printf("Worker %d: Error processing user: %v", id, result.Error)
					continue
				}
				log.Printf("Worker %d: Processed user: %+v", id, result.Data)
			}
		}(i)
	}

	// Cleanup
	close(users)
	wg.Wait()
}