sadf
This commit is contained in:
parent
1f098de743
commit
c14a385a8a
3
book_with_bridge/func_in_func/go.mod
Normal file
3
book_with_bridge/func_in_func/go.mod
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
module main
|
||||
|
||||
go 1.25.5
|
||||
50
book_with_bridge/func_in_func/main.go
Normal file
50
book_with_bridge/func_in_func/main.go
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func findSub(s, sub, repl string) (finStr string) {
|
||||
if len(s) < len(sub) {
|
||||
return s[:]
|
||||
}
|
||||
|
||||
found := false
|
||||
currCount := 0
|
||||
newStr := ""
|
||||
|
||||
if s[:len(sub)] == sub {
|
||||
newStr = repl
|
||||
currCount++
|
||||
found = true
|
||||
// return
|
||||
} else {
|
||||
newStr = string(s[0])
|
||||
}
|
||||
|
||||
var resStr string
|
||||
|
||||
if found {
|
||||
resStr = findSub(s[len(sub):], sub, repl)
|
||||
} else {
|
||||
resStr = findSub(s[1:],sub,repl)
|
||||
}
|
||||
|
||||
return newStr + resStr
|
||||
}
|
||||
|
||||
func expand(s,sub,repl string, f func(init string,sub string,repl string) string) string{
|
||||
res := f(s,sub,repl)
|
||||
return res
|
||||
}
|
||||
|
||||
func main() {
|
||||
str := flag.String("str", "lolkeklolkeklol", "Initital string")
|
||||
sub := flag.String("sub", "kek", "substring to find")
|
||||
repl := flag.String("repl", "puk", "String to replace with")
|
||||
|
||||
// res := findSub(*str, *sub, *repl)
|
||||
res := expand(*str,*sub,*repl,findSub)
|
||||
fmt.Println(res)
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user