How to extract go builtin function code such as delete() - go

I have found the builtin function in go src (/go/src/builtin/builtin.go), as following:
func delete(m map[Type]Type1, key Type)
But this is not source code. Who can tell me where is the builtin function source code?

builtin.go is simply a file for documentation purposes. It doesn't contain any implementations.
The map implementation is in runtime/map.go. The delete functionality is implemented in the mapdelete function.

Related

How to write example tests in Go?

I'm writing a test framework and would like to put examples in my documentation. For maintainability, I'd like to have these examples tested but I can't figure out how.
Ideally, I would like a tested example which looks like:
func TestFoo(t *testing.T) {
f := mytestframework.FromT(t)
// code using f
}
Wrapping the above snippet in func ExampleFoo() { } doesn't work as function definitions can't be nested (this is a syntax error).
I've tried putting this in a separate example_test.go file, however godoc will mistake this for a test file, as according to the go.dev blog, on whole-file examples (emphasis my own):
A whole file example is a file that ends in _test.go and contains exactly one example function, no test or benchmark functions, and at least one other package-level declaration.
I've looked at the docs for Go's doc package, but I couldn't figure out whether this was useful to me.
I could just paste the example as a markdown code block into documentation somewhere, but then this wouldn't be tested and could quietly go out of date in future.
Is there any way to have example tests tested, or at least type-checked?
As designed, the testing package doesn't support executable examples using *testing.T. Currently by definition whole file examples must not include Test* functions.
The best option is simply adding the example code as an indented block to the package or function documentation. This is how the testing package provides example documentation
// Using test framework:
//
// func TestFoo(t *testing.T) {
// framework(t)
// }
Another option would be to use Example functions and declare the test function as an anonymous function
func ExampleTesting() {
_ = func(t *testing.T) {
framework(t)
}
}

Cypress command vs JS function

The Cypress documentation suggests that commands are the right way to reuse fragments of code, e.g.
Cypress.Commands.add("logout", () => {
cy.get("[data-cy=profile-picture]").click();
cy.contains("Logout").click();
});
cy.logout();
For simple cases like this, why would I use a command over a plain JS function (and all the nice IDE assistance that comes with it). What are the drawbacks of rewriting the above snippet as
export function logout(){
cy.get("[data-cy=profile-picture]").click();
cy.contains("Logout").click();
}
// and now somewhere in a test
logout();
Based on my experience with Cypress (one year project and several hundred test cases), I can say that a plan JS function is great for grouping cy commands.
From my point of view, a custom cy command may be really useful only if it is incorporated into the chain processing (utilizes the subject parameter or returns a Chainable to be used further in the chain). Otherwise, a plain JS function is preferable due to it simplicity and full IDE support (unless you're using an additional plugin).
If you for any reason need to do something inside the cypress loop, you can always wrap you code by cy.then() in a plain JS function:
function myFunction() {
cy.then(() => {
console.log(("I'm inside the Cypress event loop"))
})
}
Commands are for behavior that is needed across all tests. For example, cy.setup or cy.login. Otherwise, use functions.
See official docs: https://docs.cypress.io/api/cypress-api/custom-commands#1-Don-t-make-everything-a-custom-command

how to get g in golang MPG model

I'm reading some runtime of code of golang(go1.6.2 linux/amd64), Could someone help me to understand the underlying mechanism of getg() function in runtime/stubs.go file?
// getg returns the pointer to the current g.
// The compiler rewrites calls to this function into instructions
// that fetch the g directly (from TLS or from the dedicated register).
func getg() *g
how do the getg() function operate here? What is the body of this function?
See Function signature with no function body, and the specification for Function Declarations.
In the case of runtime.getg the code is directly emitted by the compiler.
It's not being executed—exactly because, as stated in the comment
The compiler rewrites calls to this function into instructions
that fetch the g directly (from TLS or from the dedicated register).
As you can obtain yourself via something like
grep -rFw getg /usr/share/go-1.7/src/
the code which is emitted when the compiler sees a call to runtime.getg()
is architecture-dependent and is located in the files
{src}/src/cmd/compile/internal/{arch}/ggen.go
(for Go 1.7)—where {src} is the source directory of your Go code
and {arch} is an architecture-specific directory.
Say, for amd64, it's {src}/cmd/compile/internal/amd64/ggen.go.

How can I pass a local variable from function to event listener function in JavaScript?

Good day!
I began writing my own basic JavaScript library for personal use and distribution a few days ago, but I am having trouble with one of the methods, specifically bind().
Within the method itself, this refers to the library, the object.
I went to Google and found function.call(), but it didn't work out the way I planned it--it just executed the function.
If you take a look at another method, each(), you'll see that it uses call() to pass values.
I also tried the following:
f.arguments[0]=this;
My console throws an error, saying it cannot read '0' of "undefined".
I would like to be able to pass this (referencing the library--NOT THE WINDOW) to use it in the event listener.
You can see it starting at line 195 of the JavaScript of this JSFiddle.
Here it is as well:
bind:function(e,f){
if(e.indexOf("on")==0){
e=e.replace("on","");
}
if(typeof f==='function'){
/*Right now, 'this' refers to the library
How can I pass the library to the upcoming eventListener?
*/
//f=f(this); doesn't work
//f.call(this); //doesn't work
//this.target refers to the HTMLElement Object itself, which we are adding the eventListener to
//the outcome I'm looking for is something like this:
/*$('h3').which(0).bind(function({
this.css("color:red");
});*/
//(which() defines which H3 element we're dealing with
//bind is to add an event listener
this.target.addEventListener(e,f,false)
}
return this;
},
Thank you so much for your help, contributors!
If, as per your comments, you don't want to use .bind(), rather than directly passing f to addEventListener() you could pass another function that in turn calls f with .call() or .apply():
if(typeof f==='function'){
var _this = this;
this.target.addEventListener(e,function(event){
f.call(_this, event);
},false)
}
Doing it this way also lets your library do any extra event admin, e.g., pre-processing on the event object to normalise properties that are different for different browsers.
So in this particular case you actually want to call JavaScript's built in bind method that all functions have.
f = f.bind(this);
f will be a new function with it's this argument set to whatever you passed into it.
Replace f=f(this); with f.apply(this);
Look at underscore code, here:
https://github.com/jashkenas/underscore/blob/master/underscore.js#L596

Is there a method like GetDeviceDescriptorPtr() in Mac?

There is a function GetConfigurationDescriptorPtr, for getting pointer to configuration descriptor, inside IOUSBDeviceInterface. Link for reference is given below.
http://developer.apple.com/library/mac/#documentation/Darwin/Reference/IOKit/IOUSBLib_h/Classes/IOUSBDeviceInterface/index.html#//apple_ref/doc/com/intfm/IOUSBDeviceInterface/GetConfigurationDescriptorPtr
Is there any such function like GetDeviceDecriptorPtr for getting pointer to device descriptor?
Thanks in advance..
There is no such function, but we can create such function when we study the source code of USBProber.
Here is link to download the source code of IOUSBFamily, with USBProber inside it. http://opensource.apple.com/tarballs/IOUSBFamily/

Resources