最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

go中實現(xiàn)字符切片和字符串互轉(zhuǎn)

 更新時間:2023年11月20日 09:47:50   作者:~kiss~  
這篇文章主要為大家詳細介紹了go語言中如何實現(xiàn)字符切片和字符串互轉(zhuǎn),文中的示例代碼講解詳細,具有一定的學習價值,感興趣的小伙伴可以了解一下

Go 1.21

// 返回一個Slice,它的底層數(shù)組自ptr開始,長度和容量都是len
func Slice(ptr *ArbitraryType, len IntegerType) []ArbitraryType
// 返回一個指針,指向底層的數(shù)組
func SliceData(slice []ArbitraryType) *ArbitraryType
// 生成一個字符串,底層的數(shù)組開始自ptr,長度是len
// returns a string value whose underlying bytes start at ptr and whose length is len
// The len argument must be of integer type or an untyped constant
// A constant len argument must be non-negative and representable by a value of type int
// if it is an untyped constant it is given type int
// At run time, if len is negative, or if ptr is nil and len is not zero, a run-time panic occurs
// Since Go strings are immutable, the bytes passed to String must not be modified afterwards
func String(ptr *byte, len IntegerType) string
// 返回字符串底層的數(shù)組
// returns a pointer to the underlying bytes of str
// For an empty string the return value is unspecified, and may be nil.
// Since Go strings are immutable, the bytes returned by StringData must not be modified.
func StringData(str string) *byte

Go 1.20

廢棄兩個類型SliceHeader和StringHeader

Go 1.19

string.SliceHeader和string.StringHeader經(jīng)常用在 slice of byte 和 string 高效互轉(zhuǎn)場景

// go1.18.3/src/reflect/value.go
// SliceHeader is the runtime representation of a slice.
// It cannot be used safely or portably and its representation may
// change in a later release.
// Moreover, the Data field is not sufficient to guarantee the data
// it references will not be garbage collected, so programs must keep
// a separate, correctly typed pointer to the underlying data.
type SliceHeader struct {                                                                                      
    Data uintptr
    Len  int
    Cap  int
}

// StringHeader is the runtime representation of a string.
// It cannot be used safely or portably and its representation may
// change in a later release.
// Moreover, the Data field is not sufficient to guarantee the data
// it references will not be garbage collected, so programs must keep
// a separate, correctly typed pointer to the underlying data.
type StringHeader struct {                                                                                     
    Data uintptr
    Len  int
}

Slice比String多一個Cap字段

兩個的數(shù)據(jù)都存儲在Data數(shù)組中

實現(xiàn)方式

方式1

string(bytes)或[]byte(str)

性能不佳

方式2

// toBytes performs unholy acts to avoid allocations
func toBytes(s string) []byte {
    return *(*[]byte)(unsafe.Pointer(&s))
}
// toString performs unholy acts to avoid allocations
func toString(b []byte) string {
    return *(*string)(unsafe.Pointer(&b))
}

方式3

func SliceByteToString(b []byte) string {
    return *(*string)(unsafe.Pointer(&b))
}
func StringToSliceByte(s string) []byte {
    x := (*[2]uintptr)(unsafe.Pointer(&s))
    h := [3]uintptr{x[0], x[1], x[1]}
    return *(*[]byte)(unsafe.Pointer(&h))
}

方式4

func Clone(s string) string {
    if len(s) == 0 {
        return ""
    }
    b := make([]byte, len(s))
    copy(b, s)
    return *(*string)(unsafe.Pointer(&b))
}

性能測試

var L = 1024 * 1024
var str = strings.Repeat("a", L)
var s = bytes.Repeat([]byte{'a'}, L)
var str2 string
var s2 []byte
func BenchmarkString2Slice(b *testing.B) {
    for i := 0; i < b.N; i++ {
        bt := []byte(str)
        if len(bt) != L {
            b.Fatal()
        }
    }
}
func BenchmarkString2SliceReflect(b *testing.B) {
    for i := 0; i < b.N; i++ {
        bt := *(*[]byte)(unsafe.Pointer(&str))
        if len(bt) != L {
            b.Fatal()
        }
    }
}
func BenchmarkString2SliceUnsafe(b *testing.B) {
    for i := 0; i < b.N; i++ {
        bt := unsafe.Slice(unsafe.StringData(str), len(str))
        if len(bt) != L {
            b.Fatal()
        }
    }
}
func BenchmarkSlice2String(b *testing.B) {
    for i := 0; i < b.N; i++ {
        ss := string(s)
        if len(ss) != L {
            b.Fatal()
        }
    }
}
func BenchmarkSlice2StringReflect(b *testing.B) {
    for i := 0; i < b.N; i++ {
        ss := *(*string)(unsafe.Pointer(&s))
        if len(ss) != L {
            b.Fatal()
        }
    }
}
func BenchmarkSlice2StringUnsafe(b *testing.B) {
    for i := 0; i < b.N; i++ {
        ss := unsafe.String(unsafe.SliceData(s), len(str))
        if len(ss) != L {
            b.Fatal()
        }
    }
}

官方出品必然是好東西,所以相信GO1.21即可

到此這篇關(guān)于go中實現(xiàn)字符切片和字符串互轉(zhuǎn)的文章就介紹到這了,更多相關(guān)go字符切片和字符串互轉(zhuǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Golang內(nèi)存泄漏場景以及解決方案詳析

    Golang內(nèi)存泄漏場景以及解決方案詳析

    golang中內(nèi)存泄露的發(fā)現(xiàn)與排查一直是來是go開發(fā)者頭疼的一件事,下面這篇文章主要給大家介紹了關(guān)于Golang內(nèi)存泄漏場景以及解決的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-01-01
  • go code review 代碼調(diào)試

    go code review 代碼調(diào)試

    這篇文章主要為大家介紹了go code review 代碼調(diào)試方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • go?time.After優(yōu)化后性能提升34%內(nèi)存減少67%

    go?time.After優(yōu)化后性能提升34%內(nèi)存減少67%

    這篇文章主要介紹了go語言time.After優(yōu)化后性能提升34%內(nèi)存減少67%實例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • go語言中使用ent做關(guān)聯(lián)查詢的示例詳解

    go語言中使用ent做關(guān)聯(lián)查詢的示例詳解

    go語言的ent框架是facebook開源的ORM框架,是go語言開發(fā)中的常用框架,而關(guān)聯(lián)查詢又是日常開發(fā)中的常見數(shù)據(jù)庫操作,故文本給出一個使用ent做關(guān)聯(lián)查詢的使用示例,需要的朋友可以參考下
    2024-02-02
  • Go 語言中程序編譯過程詳解

    Go 語言中程序編譯過程詳解

    本文旨在深入探討Go語言的編譯機制和最新的模塊管理系統(tǒng)——Go Modules,通過詳細的示例和步驟,我們將演示從簡單的 “Hello World” 程序到使用第三方庫的更復雜項目的開發(fā)過程,感興趣的朋友跟隨小編一起看看吧
    2024-05-05
  • Golang 限流器的使用和實現(xiàn)示例

    Golang 限流器的使用和實現(xiàn)示例

    這篇文章主要介紹了Golang 限流器的使用和實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-06-06
  • Go+Redis緩存設(shè)計與優(yōu)化實現(xiàn)

    Go+Redis緩存設(shè)計與優(yōu)化實現(xiàn)

    本文主要介紹了Go+Redis緩存設(shè)計與優(yōu)化實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2025-02-02
  • 淺析golang如何在多線程中避免CPU指令重排

    淺析golang如何在多線程中避免CPU指令重排

    這篇文章主要為大家詳細介紹了golang在多線程中避免CPU指令重排的相關(guān)知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-03-03
  • 詳解玩轉(zhuǎn)直播系列之消息模塊演進

    詳解玩轉(zhuǎn)直播系列之消息模塊演進

    本篇文章針對秀場直播,簡單地描述一下消息模型,說明一下我們消息模型的架構(gòu),并結(jié)合我們一年以來,通過處理不同的業(yè)務(wù)線上問題,來進行演進式的消息模型架構(gòu)的升級與調(diào)整,將此整理成文,并分享給大家
    2021-06-06
  • Go實踐反向代理ReverseProxy解析

    Go實踐反向代理ReverseProxy解析

    這篇文章主要為大家介紹了Go實踐反向代理示例ReverseProxy解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04

最新評論

三亚市| 灵璧县| 深泽县| 岳池县| 永昌县| 松阳县| 南充市| 天祝| 塔河县| 兴宁市| 福海县| 任丘市| 含山县| 黄梅县| 巫溪县| 资源县| 水城县| 富蕴县| 治多县| 贵定县| 大埔县| 洪湖市| 武宣县| 昌乐县| 东辽县| 清河县| 敖汉旗| 寿阳县| 仪征市| 尼勒克县| 梁河县| 元氏县| 东莞市| 桐城市| 宁河县| 鲁甸县| 永康市| 刚察县| 湘乡市| 大石桥市| 竹北市|