处理Go循环中的特定运行时错误

huangapple go评论85阅读模式
英文:

Handling a specific runtime error in Go loop

问题

我想在以下循环中捕获panic: runtime error: index out of range错误(在没有返回的函数内部),并将每个panic: runtime error: index out of rangeX连接到结果中:

func transform(inputString string, inputLength int) string {
	var result = ""
	for index := 0; index < inputLength; index++ {
		if string(inputString[index]) == " " {
			result = result + "%20"
		} else {
			result = result + string(inputString[index])
		}
	}
	return result
}

例如,对于inputString = Mr SmithinputLength = 10result将是Mr%20SmithXX。它有两个X,因为10 - 8 = 2

我知道我可以在从transform()返回时捕获panic,但我想在循环内部处理它,而不返回函数。

英文:

I'd like to catch panic: runtime error: index out of range in the following loop (inside function without returning) and concat X for each panic: runtime error: index out of range to the result:

func transform(inputString string, inputLength int) string {
	var result = &quot;&quot;
	for index := 0; index &lt; inputLength; index++ {
		if string(inputString[index]) == &quot; &quot; {
			result = result + &quot;%20&quot;
		} else {
			result = result + string(inputString[index])
		}
	}
	return result
}

for example for inputString = Mr Smith and inputLength = 10 the result is Mr%20SmithXX. It has two X because 10 - 8 = 2.

I know I can catch the panic in returning from transform() but I'd like to handle it inside the loop without returning the function.

答案1

得分: 0

> inputString = Mr Smith and inputLength = 10 the result is Mr%20SmithXX. It has two X because 10 - 8 = 2.


package main

import "fmt"

func transform(s string, tLen int) string {
    t := make([]byte, 0, 2*tLen)
    for _, b := range []byte(s) {
        if b == ' ' {
            t = append(t, "%20"...)
        } else {
            t = append(t, b)
        }
    }
    for x := tLen - len(s); x > 0; x-- {
        t = append(t, 'X')
    }
    return string(t)
}

func main() {
    s := "Mr Smith"
    tLen := 10
    fmt.Printf("%q %d\n", s, tLen)
    t := transform(s, tLen)
    fmt.Printf("%q\n", t)
}

https://go.dev/play/p/wg7MIO8yUzF

"Mr Smith" 10
"Mr%20SmithXX"
英文:

> inputString = Mr Smith and inputLength = 10 the result is Mr%20SmithXX. It has two X because 10 - 8 = 2.


package main

import &quot;fmt&quot;

func transform(s string, tLen int) string {
	t := make([]byte, 0, 2*tLen)
	for _, b := range []byte(s) {
		if b == &#39; &#39; {
			t = append(t, &quot;%20&quot;...)
		} else {
			t = append(t, b)
		}
	}
	for x := tLen - len(s); x &gt; 0; x-- {
		t = append(t, &#39;X&#39;)
	}
	return string(t)
}

func main() {
	s := &quot;Mr Smith&quot;
	tLen := 10
	fmt.Printf(&quot;%q %d\n&quot;, s, tLen)
	t := transform(s, tLen)
	fmt.Printf(&quot;%q\n&quot;, t)
}

https://go.dev/play/p/wg7MIO8yUzF

&quot;Mr Smith&quot; 10
&quot;Mr%20SmithXX&quot;

huangapple
  • 本文由 发表于 2022年10月22日 06:36:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/74159756.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定