当前位置:网站首页>Golang's pen test questions & interview questions 01
Golang's pen test questions & interview questions 01
2022-04-23 11:26:00 【runscript. sh】
Just a few simple written test questions , Or you can show me the results directly during the interview .
1, Using different goroutine To operate map There will be a problem of thread synchronization , hold map Switch to int , This problem also exists . stay go It's like this .
2, The output value of the following code is :
func t(){
jsonStr:=[]byte(`{"age":1}`)
var value map[string]interface{
}
json.Unmarshal(jsonStr,&value)
age:=value["age"]
fmt.Println(reflect.TypeOf(age))
//float64
}
difference :
func t() {
jsonStr := []byte(`{"age":1}`)
var value map[string]int
json.Unmarshal(jsonStr, &value)
age := value["age"]
fmt.Println(reflect.TypeOf(age))
//int
}
3, Is there a problem with the code below , What's the problem
import (
"sync"
"fmt"
)
type UserAges struct {
ages map[string] int
sync.Mutex
}
func (u *UserAges)Add(name string,age int) {
u.Lock()
defer u.Unlock()
u.ages[name] = age
}
func (u *UserAges)Get(name string)int{
if age,ok:=u.ages[name];ok{
return age
}
return -1
}
The problem lies in ,ages There is no bag that exposes the outside of the back , Causes the caller to fail to initialize ages.
And then call add Function time , You're going to report a mistake . Investigate the scope of function .
4, What is the output of the following code ?
func TestArrayAndSlice(){
s1:=[]int{
1,2,3}
s2:=s1[1:]
for i:=range s2{
s2[i]+=10
}
fmt.Println(s2)
s2=append(s2, 4)
for i:=range s2{
s2[i]+=10
}
fmt.Println(s2)
}
The output is as follows : This is used to investigate arrays and slices s2 Intercept s1 Below is 1 And after ; Then perform the operation .
[12 13]
[22 23 14]
5, What does the following code output
func TestDoit(){
doit:= func(arg int) interface{
}{
var result *struct{
}=nil
if (arg>0) {
result = &struct{
}{
}
}
return result
}
// Output results .
//-1:result: <nil> Empty anonymous structure
//1://result: &{} Anonymous address structure
if res:=doit(1);res!=nil{
fmt.Println("result:",res)
}
}
output:
result: &{
}
6, What is the output of the following code
// Put it in main inside
// Specifies that only one logical processor can be used , It is convenient to see the scheduling sequence .
func t2(){
runtime.GOMAXPROCS(1)
wg:=sync.WaitGroup{
}
wg.Add(20)
//step 1
for i:=0;i<10 ;i++ {
go func() {
fmt.Println("i",i)
wg.Done()
}()
}
fmt.Println("--------------------")
//step 2
for j:=0;j<10 ;j++ {
go func(j int) {
fmt.Println("j",j)
wg.Done()
}(j)
}
wg.Wait()
}
output:
--------------------
j 9
i 10
i 10
i 10
i 10
i 10
i 10
i 10
i 10
i 10
i 10
j 0
j 1
j 2
j 3
j 4
j 5
j 6
j 7
j 8
This is something to watch out for , first for There are no parameters in the , The second parameter is passed .
So the first one for It's started in the library goroutine With i It's actually in the main thread i;
The reason is 10,( It's also possible that the first few <10); Because it calls i When ,i Has been added to the main thread 10 了 .
And the second one. for Inside i Is passed through parameters , So it will print 0~9;
版权声明
本文为[runscript. sh]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231126068630.html
边栏推荐
- Redis optimization series (II) redis master-slave principle and master-slave common configuration
- Résumé de la relation entre GPU, cuda et cudnn
- 云呐|如何管理好公司的固定资产,固定资产管理怎么做
- qt5. 8. You want to use SQLite in the 64 bit static library, but the static library has no method to compile the supporting library
- Usage record of map < qstring, bool >
- Who said you should know PS? This open-source artifact can also be pulled in batch, and the effect is outstanding!
- mysql分表之后如何平滑上线详解
- GPU, CUDA,cuDNN三者的关系总结
- 简易投票系统数据库设计
- MQ在laravel中简单使用
猜你喜欢
R-Drop:更强大的Dropout正则方法
初探 Lambda Powertools TypeScript
PDMS soft lithography process
nacos基础(5):nacos配置入门
Laravel adds custom helper functions
Redis学习之五---高并发分布式锁实战
VM set up static virtual machine
实践数据湖iceberg 第三十课 mysql->iceberg,不同客户端有时区问题
GPU, CUDA,cuDNN三者的关系总结
On the integration of steam education in early childhood education
随机推荐
Analyzing the role of social robots in basic science
论坛系统数据库设计
采用百度飞桨EasyDL完成指定目标识别
进程间通信 -- 消息队列
Laravel增加自定义助手函数
分享两个实用的shell脚本
Laravel always returns JSON response
How to quickly query 10 million pieces of data in MySQL
Write console script by laravel
Laravel admin time range selector daterange default value problem
谁说抠图要会 PS?这个开源神器还能批量抠,效果拔群!
laravel 永远返回 JSON 响应
Mysql系列SQL查询语句书写顺序及执行顺序详解
卷积层和池化层总结
redis优化系列(二)Redis主从原理、主从常用配置
Overall plan management mode in maker Education
Laravel绑定钉钉群警报(php)
Summary of the relationship among GPU, CUDA and cudnn
Laravel admin form validation
MySQL索引优化之分页探索详细介绍