style(go): fix go code style

Make the classes and methods in the package private, in case misuse
This commit is contained in:
reanon
2023-01-03 14:39:31 +08:00
parent e8f7d8f8ba
commit b73ac7bf4b
18 changed files with 255 additions and 254 deletions
@@ -5,7 +5,7 @@
package chapter_array_and_linkedlist
/* 列表类简易实现 */
type MyList struct {
type myList struct {
numsCapacity int
nums []int
numsSize int
@@ -13,8 +13,8 @@ type MyList struct {
}
/* 构造函数 */
func newMyList() *MyList {
return &MyList{
func newMyList() *myList {
return &myList{
numsCapacity: 10, // 列表容量
nums: make([]int, 10), // 数组(存储列表元素)
numsSize: 0, // 列表长度(即当前元素数量)
@@ -23,17 +23,17 @@ func newMyList() *MyList {
}
/* 获取列表长度(即当前元素数量) */
func (l *MyList) size() int {
func (l *myList) size() int {
return l.numsSize
}
/* 获取列表容量 */
func (l *MyList) capacity() int {
func (l *myList) capacity() int {
return l.numsCapacity
}
/* 访问元素 */
func (l *MyList) get(index int) int {
func (l *myList) get(index int) int {
// 索引如果越界则抛出异常,下同
if index >= l.numsSize {
panic("索引越界")
@@ -42,7 +42,7 @@ func (l *MyList) get(index int) int {
}
/* 更新元素 */
func (l *MyList) set(num, index int) {
func (l *myList) set(num, index int) {
if index >= l.numsSize {
panic("索引越界")
}
@@ -50,7 +50,7 @@ func (l *MyList) set(num, index int) {
}
/* 尾部添加元素 */
func (l *MyList) add(num int) {
func (l *myList) add(num int) {
// 元素数量超出容量时,触发扩容机制
if l.numsSize == l.numsCapacity {
l.extendCapacity()
@@ -61,7 +61,7 @@ func (l *MyList) add(num int) {
}
/* 中间插入元素 */
func (l *MyList) insert(num, index int) {
func (l *myList) insert(num, index int) {
if index >= l.numsSize {
panic("索引越界")
}
@@ -79,7 +79,7 @@ func (l *MyList) insert(num, index int) {
}
/* 删除元素 */
func (l *MyList) remove(index int) int {
func (l *myList) remove(index int) int {
if index >= l.numsSize {
panic("索引越界")
}
@@ -95,7 +95,7 @@ func (l *MyList) remove(index int) int {
}
/* 列表扩容 */
func (l *MyList) extendCapacity() {
func (l *myList) extendCapacity() {
// 新建一个长度为 self.__size 的数组,并将原数组拷贝到新数组
l.nums = append(l.nums, make([]int, l.numsCapacity*(l.extendRatio-1))...)
// 更新列表容量
@@ -103,7 +103,7 @@ func (l *MyList) extendCapacity() {
}
/* 返回有效长度的列表 */
func (l *MyList) toArray() []int {
func (l *myList) toArray() []int {
// 仅转换有效长度范围内的列表元素
return l.nums[:l.numsSize]
}