当前位置:网站首页>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
边栏推荐
猜你喜欢

Interpretation of biological recognition in robot programming course

R-drop: a more powerful dropout regularization method

MQ在laravel中简单使用

docker MySQL主从备份

Analyzing the role of social robots in basic science

云呐|固定资产盘点中,支持多种盘点方式(资产清查盘点)

讯飞2021年营收183亿:同比增41% 净利为15.56亿

初探 Lambda Powertools TypeScript

Get things technology network optimization - CDN resource request Optimization Practice

nacos基础(9):nacos配置管理之从单体架构到微服务
随机推荐
Summary of QT semaphore unresolved errors
学习 Go 语言 0x01:从官网开始
项目实训-火爆辣椒
解决 『SunCertPathBuilderException:unable to find valid certification path to requested target』 问题
Detailed explanation of writing sequence and execution sequence of MySQL series SQL query statements
Résumé de la relation entre GPU, cuda et cudnn
得物技术网络优化-CDN资源请求优化实践
TclError: no display name and no $DISPLAY environment variable
QT 64 bit static version display gif
Learn go language 0x06: Fibonacci closure exercise code in go language journey
解析性能良好的机器人使用守则
redis优化系列(二)Redis主从原理、主从常用配置
R-Drop:更强大的Dropout正则方法
云呐|固定资产盘点中,支持多种盘点方式(资产清查盘点)
Interpretation of 2022 robot education industry analysis report
Significance of actively participating in middle school robot competition
Overall plan management mode in maker Education
谁说抠图要会 PS?这个开源神器还能批量抠,效果拔群!
Implementation of inserting millions of data into MySQL database in 10 seconds
少儿编程结构的改变之路