浅谈 go slice & string

浅谈 Slice

array

array 是固定大小的,256 is part of the type

var buffer [256]byte

In go, the most common purpose for array is to hold storage for a slice

Slice basics

desc from the official blog :

A slice is a data structure describing a contiguous section of an array stored separately from the slice variable itself. A slice is not an array. A slice describes a piece of an array.

slice := buffer[100:150]

In fact, slice is a struct value holding a pointer and a length

A go-style snippet to create a slice

func main(){
    var arr1 [5]int
    arr2 := [5]int{1, 2, 3, 4, 5}   //指定长度为5,并赋5个初始值
    arr3 := [5]int{1, 2, 3}         //指定长度为5,对前3个元素进行赋值,其他元素为零值
    arr4 := [5]int{4: 1}            //指定长度为5,对第5个元素赋值
    arr5 := [...]int{1, 2, 3, 4, 5} //不指定长度,对数组赋以5个值
    arr6 := [...]int{8: 1}          //不指定长度,对第9个元素(下标为8)赋值1
    fmt.Println(arr1, arr2, arr3, arr4, arr5, arr6)
}

Modify a slice

  1. by using pointer
func PtrSubtractOneFromLength(slicePtr *[]byte) {
    slice := *slicePtr
    *slicePtr = slice[0 : len(slice)-1]
}
  1. by return a modified slice
func SubtractOneFromLength(slice []byte) []byte {
    slice = slice[0 : len(slice)-1]
    return slice
}

The capacity

The Capacity field records how much space the underlying array actually has; it is the maximum value the Length can reach. Trying to grow the slice beyond its capacity will step beyond the limits of the array and will trigger a panic.

Append

A few interesting examples

// Add an item to a slice.
slice = append(slice, 4)
fmt.Println("Add one item:", slice)

// Add one slice to another.
slice = append(slice, slice2...)
fmt.Println("Add one slice:", slice)

// Make a copy of a slice (of int).
slice3 := append([]int(nil), slice...)
fmt.Println("Copy a slice:", slice3)

Randomly delete element

Go 并没有给随机元素删除提供语法糖,毕竟这是个O(n)的操作
可以用slice拼接的方法来做:

a := []int{1,2,3,4}
deletePos := 2
a = append(a[0:deletePos],a[deletePos+1:]...)

浅谈 String

string basics

From the official blog…

Strings are actually very simple: they are just read-only slices of bytes with a bit of extra syntactic support from the language.

In addition :

  • Go source code is always UTF-8.
  • A string holds arbitrary bytes.
  • A string literal, absent byte-level escapes, always holds valid UTF-8 sequences.
  • Those sequences represent Unicode code points, called runes.
  • No guarantee is made in Go that characters in strings are normalized.

print a string

escape characters in Go

//general
%v	the value in a default format
	when printing structs, the plus flag (%+v) adds field names
%#v	a Go-syntax representation of the value
%T	a Go-syntax representation of the type of the value
%%	a literal percent sign; consumes no value
//string
%s	the uninterpreted bytes of the string or slice
%q	a double-quoted string safely escaped with Go syntax
%x	base 16, lower-case, two characters per byte
%X	base 16, upper-case, two characters per byte
//integer
%b	base 2
%c	the character represented by the corresponding Unicode code point
%d	base 10
%o	base 8
%O	base 8 with 0o prefix
%q	a single-quoted character literal safely escaped with Go syntax.
%x	base 16, with lower-case letters for a-f
%X	base 16, with upper-case letters for A-F
%U	Unicode format: U+1234; same as "U+%04X"
//special
%f     default width, default precision
%9f    width 9, default precision
%.2f   default width, precision 2
%9.2f  width 9, precision 2
%9.f   width 9, precision 0
const sample = "\xbd\xb2\x3d\xbc\x20\xe2\x8c\x98"
fmt.Printf("% x\n", sample)
// bd b2 3d bc 20 e2 8c 98

Go source code is UTF8, so range will iterate over UTF8(not byte!)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值