blob: d895205a5c46a6aeddb09e60a374e136cb7178a3 (
plain)
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
|
package main
import (
"fmt"
"runtime"
"strings"
"unicode"
)
func hello_world(action string) {
// 1. Get the Program Counter (PC) of the current function
pc, _, _, _ := runtime.Caller(0)
// 2. Get the full function name (e.g., "main.hello_world")
fullFuncName := runtime.FuncForPC(pc).Name()
// 3. Strip the package path to get just "hello_world"
// Splits "main.hello_world" and takes the last part
parts := strings.Split(fullFuncName, ".")
funcName := parts[len(parts)-1]
// 4. Split into words: "hello", "world"
words := strings.Split(funcName, "_")
// 5. Capitalize each word: "Hello", "World"
for i, w := range words {
runes := []rune(w)
runes[0] = unicode.ToUpper(runes[0])
words[i] = string(runes)
}
// 6. Join with ", " and add "!" -> "Hello, World!"
finalString := strings.Join(words, ", ") + "!"
// 7. Execute the action
if action == "print" {
fmt.Println(finalString)
}
}
func main() {
hello_world("print")
}
|