onclick如何调用含参函数_在 golang 中如何调用私有函数(绑定隐藏的标识符)
名字在 golang 中的重要性和在其他任何一種語言是一樣的。他們甚至含有語義的作用:在一個包的外部某個名字的可見性是由這個名字首字母是否是大寫來決定的。
有時為了更好的組織代碼或者在其他包使用某些隱藏的函數時需要克服這種限制。
在過去美好的日子,有 2 種實現方式,它們能繞過編譯器的檢查:不能引用未導出的名稱 pkg.symbol :
舊的方式,現在已經不再使用 - 匯編級隱式連接到所需符號,稱為 assembly stubs ,詳見 go runtime, os/signal: use //go:linkname instead of assembly stubs to get access to runtime functions 。
- 現行的方式 - go 編譯器通過 go:linkname 支持名稱重定向,引用于 11.11.14 dev.cc code review 169360043: cmd/gc: changes for removing runtime C code (issue 169360043 by r…@golang.org) ,在 http://github.com 的 issue 上有可以找到 cmd/compile: “missing function body” error when using the //go:linkname compiler directive #15006 。
用這些技巧我曾設法綁定 golang 運行時調度器相關的函數用以減少過度使用 go 的協程和內部鎖機制導致的 gc 停頓。
使用 assembly stubs
想法很簡單 - 為需要的標識符提供直接跳轉匯編指令 stubs 。鏈接器并不知道標識符是否已導出。
詳見舊版的代碼 src/os/signal/sig.s :
// Assembly to get into package runtime without using exported symbols. // +build amd64 amd64p32 arm arm64 386 ppc64 ppc64le#include "textflag.h"#ifdef GOARCH_arm #define JMP B #endif #ifdef GOARCH_ppc64 #define JMP BR #endif #ifdef GOARCH_ppc64le #define JMP BR #endifTEXT ·signal_disable(SB),NOSPLIT,$0 JMP runtime·signal_disable(SB)TEXT ·signal_enable(SB),NOSPLIT,$0 JMP runtime·signal_enable(SB)TEXT ·signal_ignore(SB),NOSPLIT,$0 JMP runtime·signal_ignore(SB)TEXT ·signal_recv(SB),NOSPLIT,$0 JMP runtime·signal_recv(SB)而 signal_unix.go 的綁定如下:
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris windowspackage signalimport ("os""syscall" )// In assembly. func signal_disable(uint32) func signal_enable(uint32) func signal_ignore(uint32) func signal_recv() uint32使用 go:linkname
為了使用這種方法,代碼中必須 import _ "unsafe" 包。為了解決 go 編譯器 -complete 參數的限制,一種可能的方法是在 main 包目錄加一個空的匯編 stub 文件以禁用編譯器的檢查。
詳見 os/signal/sig.s:
// The runtime package uses //go:linkname to push a few functions into this // package but we still need a .s file so the Go tool does not pass -complete // to the go tool compile so the latter does not complain about Go functions // with no bodies.這個指令的格式是 //go:linkname localname linkname。使用這種方法可以將新的標識符鏈接(導出)或綁定到已存在的標識符(導入)。
用 go:linkname 導出
在 runtime/proc.go 中一個函數的實現
...//go:linkname sync_runtime_doSpin sync.runtime_doSpin //go:nosplit func sync_runtime_doSpin() {procyield(active_spin_cnt) }這里明確地向編譯器指示,將另一個名字添加到 sync 包的 runtime_doSpin 函數代碼中。并且 sync 包簡單的重用了在 sync/runtime.go 中的代碼:
package syncimport "unsafe"...// runtime_doSpin does active spinning. func runtime_doSpin()用 go:linkname 導入
在 net/parse.go 中有一個很好的例子:
package netimport (..._ "unsafe" // For go:linkname )...// byteIndex is strings.IndexByte. It returns the index of the // first instance of c in s, or -1 if c is not present in s. // strings.IndexByte is implemented in runtime/asm_$GOARCH.s //go:linkname byteIndex strings.IndexByte func byteIndex(s string, c byte) int使用這種技巧的方法:
import _ "unsafe" 包。 提供一個沒有函數體的函數,比如: func byteIndex(s string, c byte) int 在定義函數前,正確的放上//go:linkname 指令,例如 //go:linkname byteIndex strings.IndexByte,byteIndex是本地名稱,strings.IndexByte是遠程名稱。 提供 .s 后綴的 stub 文件,以便編譯器繞過 -complete 的檢查,允許不完整的函數定義(譯注:指沒有函數體)。
例子 goparkunlock
package mainimport (_ "unsafe""fmt""runtime/pprof""os""time" )// Event types in the trace, args are given in square brackets. const (traceEvGoBlock = 20 // goroutine blocks [timestamp, stack] )type mutex struct {// Futex-based impl treats it as uint32 key,// while sema-based impl as M* waitm.// Used to be a union, but unions break precise GC.key uintptr }//go:linkname lock runtime.lock func lock(l *mutex)//go:linkname unlock runtime.unlock func unlock(l *mutex)//go:linkname goparkunlock runtime.goparkunlock func goparkunlock(lock *mutex, reason string, traceEv byte, traceskip int)func main() {l := &mutex{}go func() {lock(l)goparkunlock(l, "xxx", traceEvGoBlock, 1)}()for {pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)time.Sleep(time.Second * 1)} }源碼
可在這里獲取 https://github.com/sitano/gsysint 。
相關文章
- Docker Windows install instructions on the state of 4 August 2016 04 Aug 2016
- PowerShell ducklish typed 25 Apr 2016
- Approach into strong typed configuration management DSL with FAKE, F#, WinRM and PowerShell 15 Mar 2016
原文章鏈接:https://studygolang.com/articles/12134
總結
以上是生活随笔為你收集整理的onclick如何调用含参函数_在 golang 中如何调用私有函数(绑定隐藏的标识符)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python日志模块_Python之日志
- 下一篇: 如何把python可视化到前端_pyth