Let us say I have a string s.
s := "helloworld"
Now, my question is, if s has 'n' bytes, what is the time complexity with respect to 'n' if I pass s to a function versus if I pass &s to a function and then access the ith byte of the string.
If it takes O(1) time when I pass &s to a function and access the ith byte of the string, then will it take O(n) time when I pass s to a function and then access the ith byte of the string (because the whole string will be copied)?
I tried this and found that copying a string does indeed change the pointer to it. Would appreciate more clarity on this.
func main() {
str := "helloworld"
fmt.Println("string pointer 1:", &str)
printStringPointer(str)
}
func printStringPointer(s string) {
fmt.Println("string pointer 2:", &s)
}
Output:
string pointer 1: 0xc000010200
string pointer 2: 0xc000010210