Skip to content

Commit cccc284

Browse files
committed
docs: update docs
1 parent 21f0612 commit cccc284

File tree

2 files changed

+178
-109
lines changed

2 files changed

+178
-109
lines changed

src/en/guide/interview/golang/basic/1-basic.md

+89-56
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,72 @@ s := []string{"red", "black"}
220220
```
221221
:::
222222

223+
## Map
224+
225+
### Can an uninitialized Map read a key?
226+
227+
::: details Answer
228+
229+
Yes, an uninitialized `map` that hasn't undergone `make` initialization will return the zero value of the current type for any `key` read.
230+
231+
```go
232+
package main
233+
234+
import "fmt"
235+
236+
func main() {
237+
var m map[int]int
238+
239+
fmt.Println(m[1])
240+
}
241+
242+
// Output:
243+
// 0
244+
```
245+
:::
246+
247+
### What happens if you assign a value to an uninitialized Map?
248+
249+
::: details Answer
250+
251+
It will trigger a `panic` exception error.
252+
253+
```go
254+
package main
255+
256+
func main() {
257+
var m map[int]int
258+
259+
m[1] = 1
260+
}
261+
262+
// Output:
263+
// panic: assignment to entry in nil map
264+
```
265+
266+
:::
267+
268+
### What happens if you delete a key from an uninitialized Map?
269+
270+
::: details Answer
271+
272+
In earlier versions, performing a `delete` operation on an uninitialized `map` would throw a `panic` error. In current versions, performing a `delete` operation on an uninitialized `map` will not cause an error.
273+
274+
```go
275+
package main
276+
277+
func main() {
278+
var m map[int]int
279+
280+
delete(m, 1)
281+
}
282+
283+
// Output:
284+
//
285+
```
286+
287+
:::
288+
223289

224290
## Others
225291
### What is the difference between `rune` and `byte` in Go?
@@ -491,69 +557,36 @@ The advantage of using pointer transfer is that it directly transfers the addres
491557

492558
:::
493559

560+
### Golang Common String Concatenation Methods and Their Efficiency
494561

495-
## Map
496-
497-
### Can an uninitialized Map read a key?
498-
499-
::: details Answer
500-
501-
Yes, an uninitialized `map` that hasn't undergone `make` initialization will return the zero value of the current type for any `key` read.
502-
503-
```go
504-
package main
505-
506-
import "fmt"
507-
508-
func main() {
509-
var m map[int]int
510-
511-
fmt.Println(m[1])
512-
}
513-
514-
// Output:
515-
// 0
516-
```
517-
:::
518-
519-
### What happens if you assign a value to an uninitialized Map?
520-
521-
::: details Answer
562+
::: details
522563

523-
It will trigger a `panic` exception error.
564+
| Method | Description |
565+
| ----------------- | --------------------------------------------------------------------------------------------------------------------------------- |
566+
| `+` | Using the `+` operator for concatenation involves iterating over the strings, calculating, and allocating new space. |
567+
| `fmt.Sprintf` | Since `printf` allows format specifiers like `%d`, `sprintf` uses reflection to convert different types, which is less efficient. |
568+
| `strings.Builder` | Concatenation using `WriteString()` internally uses `[]byte` slices and `unsafe.pointer` pointers. |
569+
| `bytes.Buffer` | A byte buffer, underlyingly based on `[]byte` slices. |
570+
| `strings.Join` | `strings.Join` is implemented based on `strings.Builder`, preallocating memory space with `b.Grow(n)`, making it more efficient. |
524571

525-
```go
526-
package main
572+
> [!important]
573+
> What's the difference between `strings.Builder` and `bytes.Buffer`?
574+
> 1. `strings.Builder` preallocates space, reducing reallocations and improving efficiency, suitable for longer string concatenations.
575+
> 2. `bytes.Buffer` is mainly used for handling individual bytes, offering operations like deletion and replacement that `strings.Builder` lacks.
527576
528-
func main() {
529-
var m map[int]int
530-
531-
m[1] = 1
532-
}
533-
534-
// Output:
535-
// panic: assignment to entry in nil map
536-
```
577+
> [!tip]
578+
> Efficiency ranking:
579+
> `strings.Join``strings.Builder` > `bytes.Buffer` > `+` > `fmt.Sprintf`
537580
538581
:::
539582

540-
### What happens if you delete a key from an uninitialized Map?
541-
542-
::: details Answer
543-
544-
In earlier versions, performing a `delete` operation on an uninitialized `map` would throw a `panic` error. In current versions, performing a `delete` operation on an uninitialized `map` will not cause an error.
583+
### What Are Tags Used for in Golang?
545584

546-
```go
547-
package main
585+
::: details
548586

549-
func main() {
550-
var m map[int]int
551-
552-
delete(m, 1)
553-
}
554-
555-
// Output:
556-
//
557-
```
587+
In Go, struct fields can have various custom tags. When parsing a struct, these tags can be extracted for convenient operations. Common tags include:
558588

559-
:::
589+
- `json`: Used to declare JSON serialization and deserialization operations, specifying fields and options.
590+
- `db`: Primarily used for database field configuration, often in libraries like sqlx.
591+
- `form`: Commonly used in web frameworks to declare form field bindings.
592+
- `validate`: Frequently used for field validation rules by validators.

src/guide/interview/golang/basic/1-basic.md

+89-53
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,72 @@ s := []string{"red", "black"}
275275
```
276276
:::
277277

278+
## Map
279+
280+
### 未初始化的 Map 可以读取 key 吗?
281+
282+
::: details 答案
283+
284+
可以的,未执行 `make` 初始化的 `map` 读取任何 `key` 都会返回当前类型的空值
285+
286+
```go
287+
package main
288+
289+
import "fmt"
290+
291+
func main() {
292+
var m map[int]int
293+
294+
fmt.Println(m[1])
295+
}
296+
297+
// 结果:
298+
// 0
299+
```
300+
:::
301+
302+
### 如果对未初始化的 Map 赋值会怎么样?
303+
304+
::: details 答案
305+
306+
会触发 `panic` 异常错误
307+
308+
```go
309+
package main
310+
311+
func main() {
312+
var m map[int]int
313+
314+
m[1] = 1
315+
}
316+
317+
// 结果:
318+
// panic: assignment to entry in nil map
319+
```
320+
321+
:::
322+
323+
### 如果对未初始化的 Map 进行删除 key 的操作会发生什么?
324+
325+
::: details 答案
326+
327+
早期如果对未初始化的 `map` 进行 `delete` 操作会报 `panic` 错误, 现在的版本对于未初始化的 `map` 进行 `delete` 是不会报错的。
328+
329+
```go
330+
package main
331+
332+
func main() {
333+
var m map[int]int
334+
335+
delete(m, 1)
336+
}
337+
338+
// 结果:
339+
//
340+
```
341+
342+
:::
343+
278344

279345
## 其他
280346
### Go 中的 `rune``byte` 有什么区别?
@@ -549,68 +615,38 @@ func main() {
549615

550616
:::
551617

552-
## Map
618+
### Golang 常见的字符串拼接方式有哪些?效率有何不同?
553619

554-
### 未初始化的 Map 可以读取 key 吗?
555-
556-
::: details 答案
557-
558-
可以的,未执行 `make` 初始化的 `map` 读取任何 `key` 都会返回当前类型的空值
559-
560-
```go
561-
package main
620+
::: details
562621

563-
import "fmt"
622+
| 方法 | 描述 |
623+
| ----------------- | ---------------------------------------------------------------------------------------------------------------------- |
624+
| `+` | 使用 `+` 操作符进行拼接会对遍历字符串,计算并开辟一个新的空间来存储合并后的字符串 |
625+
| `fmt.Sprintf` | 由于 `printf` 中可以使用 `%d` 等表示变量类型, `sprintf` 需要使用到反射来将不同的类型进行转换,效率较低 |
626+
| `strings.Builder` | 使用 `WriteString()` 进行拼接操作,内部使用 `[]byte` 切片和 `unsafe.pointer` 指针实现 |
627+
| `bytes.Buffer` | `byte` 缓冲器,底层是 `[]byte` 切片 |
628+
| `strings.Join` | `strings.Join` 是基于 `strings.Builder` 来实现的,在 `Join` 方法内调用了 `b.Grow(n)` 方法, 预分配了内存空间,较为高效 |
564629

565-
func main() {
566-
var m map[int]int
630+
> [!important]
631+
> `strings.Builder``bytes.Buffer` 有什么区别?
632+
> 1. `strings.Builder` 会预分配空间,减少扩容,效率更高,适合较长的字符串拼接操作
633+
> 2. `bytes.Buffer` 主要用于处理单个字符,拥有许多针对单个 `byte` 的操作,如删除替换等,这个是 `strings.Builder` 没有的。
567634
568-
fmt.Println(m[1])
569-
}
635+
> [!tip]
636+
> 效率排行
637+
> strings.Join ≈ strings.Builder > bytes.Buffer > "+" > fmt.Sprintf
570638
571-
// 结果:
572-
// 0
573-
```
574639
:::
575640

576-
### 如果对未初始化的 Map 赋值会怎么样
641+
### Golang 中的 Tag 有什么用
577642

578-
::: details 答案
643+
::: details
579644

580-
会触发 `panic` 异常错误
645+
Golang 的结构体字段可以添加各类自定义的 `Tag`, 在解析结构体时可以使用函数将 `Tag` 解析出来,方便进行操作,常见的 `Tag`:
581646

582-
```go
583-
package main
584-
585-
func main() {
586-
var m map[int]int
587-
588-
m[1] = 1
589-
}
590-
591-
// 结果:
592-
// panic: assignment to entry in nil map
593-
```
594-
595-
:::
596-
597-
### 如果对未初始化的 Map 进行删除 key 的操作会发生什么?
598-
599-
::: details 答案
600-
601-
早期如果对未初始化的 `map` 进行 `delete` 操作会报 `panic` 错误, 现在的版本对于未初始化的 `map` 进行 `delete` 是不会报错的。
602-
603-
```go
604-
package main
605-
606-
func main() {
607-
var m map[int]int
608-
609-
delete(m, 1)
610-
}
611-
612-
// 结果:
613-
//
614-
```
647+
- json: json tag 主要用于声明 json 在序列化和反序列化时的操作,如字段,可选等功能
648+
- db: 主要用于声明数据库字段配置,用在 sqlx 中
649+
- form: 常用在 web 框架中用于声明接收表单字段
650+
- validate: 常用于校验器对于字段校验的配置
615651

616652
:::

0 commit comments

Comments
 (0)