Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change deprecated reflect function in unsafeString2Bytes #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

1neZe0
Copy link

@1neZe0 1neZe0 commented Aug 28, 2023

  1. Replace deprecated reflect functions:
    https://pkg.go.dev/reflect#SliceHeader
    https://pkg.go.dev/reflect#StringHeader
  2. Simpler - there's no need to manipulate or types.
  3. Faster because directly convert the type without creating temporary structures

https://pkg.go.dev/reflect#SliceHeader
https://pkg.go.dev/reflect#StringHeader
2. Simpler - there's no need to manipulate  or  types.
3. Faster because directly convert the type without creating temporary structures
bh.Cap = sh.Len
bh.Len = sh.Len
return b
return *(*[]byte)(unsafe.Pointer(&s))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you probably want

func String2ByteSlice(str string) []byte {
	if str == "" {
		return nil
	}
	return unsafe.Slice(unsafe.StringData(str), len(str))
}

(from https://go101.org/article/unsafe.html)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants