Go Language: Convert intstr.IntOrString to String
To convert a 'targetPort' variable of type 'intstr.IntOrString' to a string in Go, you can use the 'strconv' package.
Here's an example of how you can convert 'targetPort' to a string:
import (
"fmt"
"k8s.io/apimachinery/pkg/util/intstr"
"strconv"
)
func main() {
targetPort := intstr.FromInt(8080) // Assuming targetPort is of type intstr.IntOrString
strTargetPort := ""
if targetPort.Type == intstr.Int {
strTargetPort = strconv.Itoa(targetPort.IntVal)
} else if targetPort.Type == intstr.String {
strTargetPort = targetPort.StrVal
}
fmt.Println(strTargetPort)
}
In this example, the 'strconv.Itoa()' function is used to convert the integer value of 'targetPort' to a string. If 'targetPort' is of type 'String', the string value is directly assigned to 'strTargetPort'.
Note that the code assumes you have imported the required packages ('fmt', 'strconv', and 'k8s.io/apimachinery/pkg/util/intstr'). Make sure to adjust the package imports based on your specific code setup.
原文地址: https://www.cveoy.top/t/topic/pe6Z 著作权归作者所有。请勿转载和采集!