当前位置:网站首页>golang之笔试题&面试题01
golang之笔试题&面试题01
2022-04-23 11:26:00 【runscript.sh】
就是几个简单的笔试题目,也可能面试的时候直接给看让说结果。
1, 用不同的goroutine去操作map的时候会存在线程同步的问题,把map换成int ,这个问题同样存在。在go里是这样。
2,下面这段代码输出的值为:
func t(){
jsonStr:=[]byte(`{"age":1}`)
var value map[string]interface{
}
json.Unmarshal(jsonStr,&value)
age:=value["age"]
fmt.Println(reflect.TypeOf(age))
//float64
}
区别:
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,下边代码是否有问题,问题在哪里
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
}
问题在于,ages没有暴露背外部的包,导致调用者无法初始化ages。
然后在调用add函数时,就会报错。考察函数的作用域问题。
4,下边代码的输出结果是什么?
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)
}
输出结果如下:这个是在考察数组和切片用s2截取s1的下边为1及之后;而后执行操作。
[12 13]
[22 23 14]
5,下边代码输出什么
func TestDoit(){
doit:= func(arg int) interface{
}{
var result *struct{
}=nil
if (arg>0) {
result = &struct{
}{
}
}
return result
}
//输出结果。
//-1:result: <nil> 为空的匿名结构体
//1://result: &{} 匿名结构体的地址
if res:=doit(1);res!=nil{
fmt.Println("result:",res)
}
}
output:
result: &{
}
6,下边代码的输出结果是什么
//放在main里边
//指定只能用一个逻辑处理器,方便看调度顺序。
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
这个要注意,第一个for里没有传参数,第二个传参数了。
所以第一个for里启动的goroutine用的i其实是主线程里的i;
之所以都是10,(也有可能前面几个<10);是因为调用i的时候,i在主线程内已经加到10了。
而第二个for里的i是通过参数传递,所以会打印0~9;
版权声明
本文为[runscript.sh]所创,转载请带上原文链接,感谢
https://blog.csdn.net/zhangh571354026/article/details/124359368
边栏推荐
- qt 64位静态版本显示gif
- laravel-admin时间范围选择器dateRange默认值问题
- Laravel admin form validation
- 解决由于找不到amd_ags_x64.dll,无法继续执行代码。重新安装程序可能会解决此问题,地平线(Forza Horizon 5)
- 简易投票系统数据库设计
- R-Drop:更强大的Dropout正则方法
- Understanding of fileprovider path configuration strategy
- Overall plan management mode in maker Education
- 学习 Go 语言 0x07:《Go 语言之旅》中 Stringer 练习题代码
- GPU, CUDA,cuDNN三者的关系总结
猜你喜欢

MQ is easy to use in laravel

Analyze the rules for the use of robots with good performance

nacos基础(6):nacos配置管理模型

论坛系统数据库设计

Laravel adds custom helper functions

laravel编写Console脚本

积极参与中学机器人竞赛的意义

Cygwin 中的 rename 用法

Interprocess communication -- message queue

After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before
随机推荐
Detailed explanation of MySQL creation stored procedure and function
Implementation of inserting millions of data into MySQL database in 10 seconds
得物技术网络优化-CDN资源请求优化实践
Interprocess communication -- message queue
Using Baidu PaddlePaddle EasyDL to accomplish specified target recognition
解析社交性机器人对基础科学的作用
MySQL sorting feature details
ImportError: libX11.so.6: cannot open shared object file: No such file or directory
Learn go language 0x06: Fibonacci closure exercise code in go language journey
Advanced file IO of system programming (13) -- IO multiplexing - Select
laravel-admin时间范围选择器dateRange默认值问题
MySQL partition table can be classified by month
Exploring the equipment and teaching of robot education
How to quickly query 10 million pieces of data in MySQL
MySQL8. 0 upgraded stepping on the pit Adventure
解读2022机器人教育产业分析报告
nacos基础(7):配置管理
Upgrade the functions available for cpolar intranet penetration
Mysql中有关Datetime和Timestamp的使用总结
零钱兑换II——【LeetCode】