Last active
December 28, 2022 00:19
-
-
Save takatoshiono/3b54fdef1980e66e7b8f6766cc03e405 to your computer and use it in GitHub Desktop.
CopyDigits function ref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// ref: https://blog.golang.org/go-slices-usage-and-internals | |
// 1234 <-- digits | |
import ( | |
"fmt" | |
"io/ioutil" | |
"regexp" | |
) | |
var digitRegexp = regexp.MustCompile("[0-9]+") | |
func CopyDigits(filename string) []byte { | |
b, _ := ioutil.ReadFile(filename) | |
b = digitRegexp.Find(b) | |
return append([]byte{}, b...) | |
} | |
func main() { | |
digits := CopyDigits("./copy-digits.go") | |
fmt.Println(digits) | |
} |
Thank you very much. This helped me understand how slice append works!
Is there a way to show how much memory has been saved vs the original code?
@1Mark I may be wrong, but I think you can see by checking the capacity of the slice. Here's an example that focuses on that.
package main
import (
"fmt"
"log"
"os"
)
const sAndS = "s-and-s.txt"
func main() {
data, err := os.ReadFile(sAndS)
if err != nil {
log.Fatalf("cannot open 's-and-s.txt': %v\n", err)
}
fmt.Println("Before slicing:")
fmt.Printf("\tlen(data) = %d; cap(data) = %d\n", len(data), cap(data))
data = data[0:4]
fmt.Println("After data = data[0:4]:")
fmt.Printf("\tlen(data) = %d; cap(data) = %d\n", len(data), cap(data))
data = append([]byte{}, data[0:4]...)
fmt.Println("After data = append([]byte{}, data[0:4]...):")
fmt.Printf("\tlen(data) = %d; cap(data) = %d\n", len(data), cap(data))
}
I used it on a plaintext version of a book, and the output shows how many bytes the original file has and how many bytes different slices have access to.
$ wc -c s-and-s.txt
712928 s-and-s.txt
$ ./append
Before slicing:
len(data) = 712928; cap(data) = 712929
After data = data[0:4]:
len(data) = 4; cap(data) = 712929
After data = append([]byte{}, data[0:4]...):
len(data) = 4; cap(data) = 8
You can see that reassigning with a slice expression reduces the length, but the capacity is still as large as the whole file (712929 bytes). But if you use append
and force it to create a new backing array, the result is 8 bytes rather than 712929.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was very helpful. Thank you.