const{createApp}=Vue const{createVuetify,useGoTo,useDisplay}=Vuetify var data={alert:{show:false,color:'success',text:'',timeout:0,},theme:{dark:false,},nav:{showDrawer:false,showTOC:true,tocPanel:0,tab:'account',post:{discussionId:6221,currentPage:1,targetPage:1,total:26,anchor:0,goToOptions:{container:null,duration:0,easing:'easeInOutCubic',offset:-100,},worker:null,task:[],active:[],apiLock:[],originLike:new Map([]),},related:{block:1,}},search:{width:80,text:null,loading:false,},tags:[{id:27,url:'/t/0e2e58346a045c7567770b2b3120',name:'Go',color:'#FFA726',icon:'mdi-language-go',},],posts:[{id:72919,num:0,uid:8854,content:'\u003Cp\u003E既然无需手动管理内存,像 J**a/Python/JS 那样不在语法层面上区分 primitive value 和 reference value 不是更简洁吗?为什么不支持指针运算,却还要有指针类型呢?\u003C/p\u003E',ipRegion:'',updatedByUid:0,createdAt:'2025-03-21 12:48:05',updatedAt:'2025-03-24 23:56:27',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:72920,num:1,uid:6597,content:'func Inc(a *int) {\u003Cbr\u003E(*a)++\u003Cbr\u003E}\u003Cbr\u003E\u003Cbr\u003E\u003Cbr\u003E为了解决类似这样的情况?',ipRegion:'',updatedByUid:0,createdAt:'2025-03-21 12:59:03',updatedAt:'2025-03-24 23:56:27',mentionNum:0,mentionedBy:[4,],mentionUsers:[],likeUsers:[],},{id:72921,num:2,uid:13485,content:'有没有可能是为了兼容 cgo ?',ipRegion:'',updatedByUid:0,createdAt:'2025-03-21 13:01:49',updatedAt:'2025-03-24 23:56:27',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:72922,num:3,uid:2493,content:'形如 J**a/Python/JS 的值类型和引用类型是一种「隐式规则」,所以很难说「隐式规则」和「显示指定」之间的优劣,各有各的问题,比如传递后的对象是否还是原来的引用在有些情况下不明确。',ipRegion:'',updatedByUid:0,createdAt:'2025-03-21 13:13:34',updatedAt:'2025-03-24 23:56:27',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:72923,num:4,uid:4877,content:'#1 这种直接修改值类型参数本身就是设计问题,可以加关键字让编译器去做',ipRegion:'',updatedByUid:0,createdAt:'2025-03-21 13:23:36',updatedAt:'2025-03-24 23:56:27',mentionNum:1,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:72924,num:5,uid:8884,content:'为了区分基础类型吧,基础类型都有默认值. 觉得没有做到去掉 nil 是一个遗憾',ipRegion:'',updatedByUid:0,createdAt:'2025-03-21 13:32:15',updatedAt:'2025-03-24 23:56:27',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:72925,num:6,uid:9269,content:'值类型可以是栈对象,比堆对象更高效。\u003Cbr\u003E\u003Cbr\u003EC# 的 gc 比 J**a 看上去牛逼的原因就是因为 C# 从诞生开始就支持值类型。虽然 J**a 的 GC 牛逼得多但也是逼出来的。所以保留值类型,可以让 go 程序有可能更高效。\u003Cbr\u003E\u003Cbr\u003E虽然 go 有指针逃逸分析,和自动优化堆对象,但是众所周知,go 的编译是出了名的快(简陋)\u003Cbr\u003E\u003Cbr\u003E所以有些 go 的缓存库标榜自己是 pointer-free 的(全都分配在 []value 上),因此性能比别人好。',ipRegion:'',updatedByUid:0,createdAt:'2025-03-21 13:35:30',updatedAt:'2025-03-24 23:56:27',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:72926,num:7,uid:849,content:'当然是强制程序员有意识的区分自己是在传值还是传引用。这和 gc 不 gc 的没关系。传值不影响外面,传引用会影响外面。这在并发运算时尤其重要',ipRegion:'',updatedByUid:0,createdAt:'2025-03-21 13:36:00',updatedAt:'2025-03-24 23:56:27',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:72927,num:8,uid:249,content:'问就是保留了 C 语言的风味 \u003Cimg src\u003D\"https://i.imgur.com/agAJ0Rd.png\"\u003E',ipRegion:'',updatedByUid:0,createdAt:'2025-03-21 13:42:21',updatedAt:'2025-03-24 23:56:27',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:72928,num:9,uid:2952,content:'PHP 都有指针',ipRegion:'',updatedByUid:0,createdAt:'2025-03-21 13:44:41',updatedAt:'2025-03-24 23:56:27',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:72929,num:10,uid:443,content:' C++里同时有引用和指针,在这件事上引用语义才是更直观的。为什么会选用指针而非引用的方式来区分这二者,其实还挺奇怪的',ipRegion:'',updatedByUid:0,createdAt:'2025-03-21 13:53:37',updatedAt:'2025-03-24 23:56:27',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:72930,num:11,uid:1088,content:'指针类型× 引用类型√',ipRegion:'',updatedByUid:0,createdAt:'2025-03-21 14:06:25',updatedAt:'2025-03-24 23:56:27',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:72931,num:12,uid:1004,content:'为了区分拷贝 和引用吧。',ipRegion:'',updatedByUid:0,createdAt:'2025-03-21 14:10:10',updatedAt:'2025-03-24 23:56:27',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:72932,num:13,uid:5605,content:'我猜测 go 是想成为简单的 C(自动管理内存的 C), 所以保留了 C 中的一些概念. 但是 go 也有想 J**a 一样,默认传引用的类型, map, chan. 反而我觉得 J**a 这种 class 默认传引用 `还不好`,不够灵活.',ipRegion:'',updatedByUid:0,createdAt:'2025-03-21 14:30:47',updatedAt:'2025-03-24 23:56:27',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:72933,num:14,uid:12871,content:'因为指针可以是 null, 但是引用或者值不能是空. \u003Cbr\u003E估计是不想搞 optional, 就直接用指针了.',ipRegion:'',updatedByUid:0,createdAt:'2025-03-21 14:45:59',updatedAt:'2025-03-24 23:56:27',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:72934,num:15,uid:13479,content:'不能做运算的指针,其实就是别的语言的引用。只有叫法或者语法上的一点差异了',ipRegion:'',updatedByUid:0,createdAt:'2025-03-21 15:06:13',updatedAt:'2025-03-24 23:56:27',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:72935,num:16,uid:443,content:'确实。optional 对类型系统和后端优化的要求更高一些,Go 初版连泛型都没有,不太能做 nullable reference ,也只能上指针了。',ipRegion:'',updatedByUid:0,createdAt:'2025-03-21 15:07:06',updatedAt:'2025-03-24 23:56:27',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:72936,num:17,uid:849,content:'另外也并不是真的不能运算的指针啊,不是给你留了 unsafe.Pointer 这个口子吗',ipRegion:'',updatedByUid:0,createdAt:'2025-03-21 15:14:34',updatedAt:'2025-03-24 23:56:27',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:72937,num:18,uid:748,content:'对于用习惯 c ,迁移代码,太喜欢这点了',ipRegion:'',updatedByUid:0,createdAt:'2025-03-21 15:16:16',updatedAt:'2025-03-24 23:56:27',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:72938,num:19,uid:2525,content:'不能运算? ptr:\u003D(uintptr)((unsafe.Pointer)(\u0026amp;x));ptr\u003Dptr+unsafe.Sizeof(x)',ipRegion:'',updatedByUid:0,createdAt:'2025-03-21 15:18:05',updatedAt:'2025-03-24 23:56:27',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},],usersMap:new Map([[1004,{uid:1004,url:'/u/62785f026a045c76606a526f46073a24',avatar:'/a/62785f026a045c76606a526f46073a24',username:'wei2629🤖',}],[443,{uid:443,url:'/u/0c225d0c6a045c77646e556f133a3814',avatar:'/a/0c225d0c6a045c77646e556f133a3814',username:'InkStone🤖',}],[849,{uid:849,url:'/u/1f187e076a045c77686e5f6f141b172d',avatar:'/a/1f187e076a045c77686e5f6f141b172d',username:'strobber16🤖',}],[6597,{uid:6597,url:'/u/6d2774196a045c716563516f27101016',avatar:'/a/6d2774196a045c716563516f27101016',username:'LitterGopher🤖',}],[2525,{uid:2525,url:'/u/691964236a045c756568536f3a112a0b',avatar:'/a/691964236a045c756568536f3a112a0b',username:'voidmnwzp🤖',}],[13479,{uid:13479,url:'/u/160151586a045d74646d5f6f32100235',avatar:'/a/160151586a045d74646d5f6f32100235',username:'ccpp132🤖',}],[2952,{uid:2952,url:'/u/347b0b2f6a045c75696f546f4e0a241d',avatar:'/a/347b0b2f6a045c75696f546f4e0a241d',username:'shiny🤖',}],[8854,{uid:8854,url:'/u/112f660e6a045c7f686f526f3f066273',avatar:'/a/112f660e6a045c7f686f526f3f066273',username:'superhot🤖',}],[748,{uid:748,url:'/u/2c00063a6a045c77676e5e6f163c061e',avatar:'/a/2c00063a6a045c77676e5e6f163c061e',username:'lloovve🤖',}],[5605,{uid:5605,url:'/u/142a041e6a045c72666a536f45503870',avatar:'/a/142a041e6a045c72666a536f45503870',username:'bruce0🤖',}],[4877,{uid:4877,url:'/u/3c205f2e6a045c73686d516f31081a37',avatar:'/a/3c205f2e6a045c73686d516f31081a37',username:'jiangzm🤖',}],[2493,{uid:2493,url:'/u/3500505c6a045c756463556f06242103',avatar:'/a/3500505c6a045c756463556f06242103',username:'songray🤖',}],[249,{uid:249,url:'/u/1f0057296a045c77626e5f6f2121260c',avatar:'/a/1f0057296a045c77626e5f6f2121260c',username:'minami🤖',}],[1088,{uid:1088,url:'/u/0e2b03356a045c7660625e6f101f2323',avatar:'/a/0e2b03356a045c7660625e6f101f2323',username:'whyso🤖',}],[13485,{uid:13485,url:'/u/2b246a5c6a045d746462536f3a250715',avatar:'/a/2b246a5c6a045d746462536f3a250715',username:'pathletboy🤖',}],[12871,{uid:12871,url:'/u/6c3971216a045d75686d576f1639192c',avatar:'/a/6c3971216a045d75686d576f1639192c',username:'MoYi123🤖',}],[9269,{uid:9269,url:'/u/6a707f356a045c7e626c5f6f0d300405',avatar:'/a/6a707f356a045c7e626c5f6f0d300405',username:'ipwx🤖',}],[8884,{uid:8884,url:'/u/6c3b5f386a045c7f6862526f15211a26',avatar:'/a/6c3b5f386a045c7f6862526f15211a26',username:'chor02🤖',}],]),related:[{title:'大佬们平时都是怎样调试查看变量变化的? go 有哪些方便实时展示变量的库或工具?',url:'/d/221f67216a045c77606a567a455c636a23050963',},{title:'Cursor Go 开发,有哪些必装的插件?',url:'/d/337a733f6a045c77606a56754f5b6b6a39061e13',},{title:'在 Go 项目里,涉及到金额计算,大家一般用什么方式?',url:'/d/30267d146a045c77606a5675425d606a3f732227',},{title:'[全职]广州 - 潮汐 App - Golang 开发工程师',url:'/d/022a75356a045c77606a5675445d666a3b301504',},{title:'goland 如何远程开发?',url:'/d/1e1845056a045c77606a56754550676a29726b65',},{title:'开发了一个开箱即用的本地终端 Ztty',url:'/d/0c045a2e6a045c77606a5675455e6b6a3e200734',},{title:'go 部署到 1panel 上,是用源码运行 还是编译二进制运行!',url:'/d/193a435c6a045c77606a5675455c626a38360005',},{title:'[go 分布式定时任务框架] xxljob',url:'/d/357904216a045c77606a5675475f636a041a1663',},{title:'关于 Go 并发获取数据的问题,请教大家',url:'/d/687d67226a045c77606a5674415b646a1d3a3138',},{title:'macOS Goland 经常在双击 shift 全局搜索的时候,出现彩虹球,只能强制退出',url:'/d/380f58206a045c77606a5674435b666a15111c2a',},{title:'今天有个面试官和我讲 go 的协程比系统的线程更慢,这个我不能理解',url:'/d/182459046a045c77606a56744359626a1412041a',},{title:'Java 和 go 都会,以后找工作偏向哪个方面好呢',url:'/d/000b7b1b6a045c77606a5674445e656a1977262a',},{title:'作为一个传统开发者,如何使用 ai 或者机器学习实现一些简单事情',url:'/d/34015f186a045c77606a5674445e626a25016611',},{title:'大家 golang 项目里目前在用哪个 log 库打印日志呢?',url:'/d/623a67086a045c77606a5674455c646a5d0c1f65',},{title:'朋友们,有哪些基于 Gin 框架的优秀开源项目,用来学习?',url:'/d/28116a596a045c77606a5674455d656a3c326135',},{title:'做个调研, go 语言,大家在用哪个 http 请求库?',url:'/d/337155556a045c77606a5674475a646a262a0b10',},{title:'[golang] 后端开发工程师|海外电商 AI+ SaaS 招长期远程兼职/全职',url:'/d/1d277d2a6a045c77606a5677425b616a1a211a39',},{title:'仓颉编程语言的这个反序列化和序列化比 go 复杂好多',url:'/d/361d472b6a045c77606a567745506b6a5a1a6160',},{title:'作为 Java 狗的我,学习 rust 的时候为啥总念着 go 的好呢?',url:'/d/6f2a02176a045c77606a5671435d636a2f121465',},{title:'原 Java 刚学习 go, 自己在写项目, 针对 go 日志方面的疑惑',url:'/d/6b2c02596a045c77606a56714359626a0f361626',},],} const App={setup(){const goTo=useGoTo() const{mdAndUp}=useDisplay() return{goTo,mdAndUp}},data(){return data;},mounted(){const themeDark=localStorage.getItem("themeDark") if(themeDark!==null){this.theme.dark=JSON.parse(themeDark)} if(this.nav.post.total>(this.nav.post.currentPage-1)*100+20){let moreLen=100 if(this.nav.post.total({id:null,num:(this.nav.post.currentPage-1)*100+v,uid:null,content:null,ipRegion:null,updatedByUid:null,createdAt:null,updatedAt:null,mentionNum:null,mentionedBy:null,mentionUsers:null,likeUsers:null,})) this.posts.push(...morePosts.slice(20))} this.workerStart() const hash=window.location.hash const match=hash.match(/#(\d+)/) if(match){const n=parseInt(match[1],10) if(n>=(this.nav.post.currentPage-1)*100&&n{this.jumpTo(n)})}} this.$nextTick(()=>{this.addHeadingIds() tocbot.init({tocSelector:'.toc',contentSelector:'#post-content-0',headingSelector:'h2, h3, h4',headingsOffset:100,scrollSmoothOffset:-100,scrollSmooth:true,collapseDepth:6,onClick:function(e){setTimeout(()=>{history.replaceState(null,'',window.location.pathname+window.location.search)},0)},}) tocbot.refresh()});},beforeUnmount(){this.workerStop() if(this.quill){this.quill.destroy() this.quill=null}},computed:{dposts(){return this.posts.slice(20);},},created(){},methods:{successAlert(msg){this.alert={show:true,color:'success',text:msg,timeout:1500,}},failureAlert(msg){this.alert={show:true,color:'error',text:msg,timeout:5000,}},flipThemeDark(){this.theme.dark=!this.theme.dark localStorage.setItem("themeDark",JSON.stringify(this.theme.dark))},toSearch(){if(!this.search.text){this.failureAlert('搜索词不能为空') return} let keywords=this.search.text.trim() if(keywords.length<1){this.failureAlert('搜索词不能为空') return} if(keywords.length>100){this.failureAlert('搜索词过长') return} this.doSearch(keywords)},toReg(){window.location.href="/reg"},toLogin(){window.location.href="/login"},toPage(){let url=window.location.href url=url.replace(/(\/\d+)?(#[0-9]+)?$/,this.nav.post.targetPage>1?`/${this.nav.post.targetPage}`:'') window.location.href=url},toLoadRelated({done}){if(this.my&&this.my.uid){this.apiLoadRelated({done})}else{done('ok')}},workerStart(){this.nav.post.worker=setInterval(()=>{this.workerLoad()},500);},workerStop(){if(this.nav.post.worker){clearInterval(this.nav.post.worker);this.nav.post.worker=null;}},async jumpTo(num){const page=Math.floor(num/100)+1 const i=num-(page-1)*100 if(page===this.nav.post.currentPage){this.goTo("#post-"+num,this.nav.post.goToOptions) if(!this.posts[i].id){const block=Math.floor(num/20)+1 this.nav.post.apiLock[block]=true await this.apiLoadPosts(block) this.$nextTick(()=>{this.goTo("#post-"+num,this.nav.post.goToOptions)})}}else{let url=window.location.href url=url.replace(/(\/\d+)?(#[0-9]+)?$/,page>1?`/${page}`:'') url=url+"#"+num window.location.href=url}},postIntersect(num){return(isIntersecting,entries,observer)=>{if(isIntersecting){this.nav.post.task.push(num) this.nav.post.active.push(num) this.nav.post.active=this.nav.post.active.filter(item=>Math.abs(item-num)<=5) this.nav.post.active.sort((a,b)=>a-b)}else{this.nav.post.active=this.nav.post.active.filter(item=>item!==num)} if(this.nav.post.active[0]){this.nav.post.anchor=this.nav.post.active[0]}else{this.nav.post.anchor=0}}},async apiLoadPosts(block){try{const response=await axios.post('/fapi/v1/post/block/'+block,{discussionId:this.nav.post.discussionId,}) if(response.data.code===0){response.data.data.posts.forEach(post=>{const i=post.num%100 Object.assign(this.posts[i],post)}) response.data.data.users.forEach(user=>{this.usersMap.set(user.uid,user)})}else{this.failureAlert('回帖数据加载失败: '+response.data.msg)}}catch(error){this.failureAlert('回帖数据加载失败: '+error)} this.nav.post.apiLock[block]=false},workerLoad(){while(this.nav.post.task.length){const num=this.nav.post.task.pop() const i=num-(this.nav.post.currentPage-1)*100 if(!this.posts[i].id){const block=Math.floor(num/20)+1 if(!this.nav.post.apiLock[block]){this.nav.post.apiLock[block]=true this.apiLoadPosts(block)}}}},getTimeInfo(t){if(!t){return ""} const now=new Date();const then=new Date(t);const diff=now-then;const minute=60*1000;const hour=minute*60;const day=hour*24;const month=day*30;const year=month*12;if(diffpost.num===num) if(!post){return "#"+num} const uid=post.uid const username=this.usersMap.get(uid)?.username if(!username){return "#"+num} return username},getUsernameByPostId(id){const post=this.posts.find(post=>post.id===id) if(!post){return "#"+this.getPostNumByPostId(id)} const uid=post.uid const username=this.usersMap.get(uid).username if(!username){return "#"+this.getPostNumByPostId(id)} return username},getPostNumByPostId(id){const post=this.posts.find(post=>post.id===id) return post.num},getPostById(id){const post=this.posts.find(post=>post.id===id) return post},getPostByNum(num){const post=this.posts.find(post=>post.num===num) return post},getAvatarByUid(uid){const avatar=this.usersMap.get(uid)?.avatar if(!avatar){return this.getRandomAvatar()} return avatar},getAvatarByPostNum(num){const post=this.posts.find(post=>post.num===num) if(!post){return this.getRandomAvatar()} const uid=post.uid return this.getAvatarByUid(uid)},getRandomAvatar(){const num=Math.floor(Math.random()*100) return "https://randomuser.me/api/portraits/men/"+num+".jpg"},getUrlByUid(uid){const url=this.usersMap.get(uid)?.url if(!url){return ""} return url},getTextByPostNum(num){const post=this.posts.find(post=>post.num===num) if(!post||!post.content){return '点击跳转到#'+num+'查看'} const parser=new DOMParser() const doc=parser.parseFromString(post.content,'text/html') const text=doc.body.textContent||'' return text.slice(0,100)},addHeadingIds(){const content=document.getElementById('post-content-0') if(!content){this.nav.showTOC=false return} const headings=content.querySelectorAll('h2, h3, h4') headings.forEach((heading,index)=>{if(!heading.id){heading.id=`toc-nav-${index}`}}) if(headings.length==0){this.nav.showTOC=false}},async doSearch(keywords){this.search.loading=true try{const response=await axios.post('/fapi/v1/search',{keywords:keywords,}) if(response.data.code===0){if(response.data.data.hash&&response.data.data.hash.length===32){window.location.href="/s/"+response.data.data.hash}else{this.failureAlert('搜索失败: 搜索服务异常')}}else{this.failureAlert('搜索失败: '+response.data.msg)}}catch(error){this.failureAlert('搜索失败: '+error)} this.search.loading=false},debounce(fn,delay){let timer=null return function(...args){if(timer)clearTimeout(timer) timer=setTimeout(()=>{fn.apply(this,args)},delay);};},},watch:{'nav.post.targetPage':{handler:async function(newV,oldV){this.toPage()},immediate:false},},} const vuetify=createVuetify({defaults:{global:{ripple:true,},},}) const app=createApp(App) app.use(vuetify).mount("#app")