Security News
PyPI’s New Archival Feature Closes a Major Security Gap
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
github.com/larrybotha/learn-go-with-tests/09-mocking
Table of Contents
functions that return multiple values define those values in parenthesis:
func MyFunc() (n int, s string, xs []int) {}
to implement an interface on a struct, the method must be implemented with a receiver:
type MyStruct struct {}
// MyStruct implements Write on the io.Writer interface
func (m MyStruct) Write(p byte[]) (n int, err error) {}
like sleep
in bash, Go has a time.Sleep
method which accepts a value of
type time.Duration
:
var sleepTime time.Duration = 5 * time.Second
time.Sleep(sleepTime)
one can use a struct to encapsulate configurations:
type CustomSleeper struct {
duration time.Duration
sleep func(time.Duration)
}
func (c CustomSleeper) sleep() {
c.sleep(c.duration)
}
func main() {
sleeper := CustomSleeper{duration: 5, sleep: time.Sleep}
}
to implement an interface method, one requires a struct on which to implement the method
an interface method can't be defined on an anonymous struct, as interface methods have to be defined using receivers, while functions on structs are equivalent to assigning a function to a variable
one can either explicitly pass a value by its address to a function, or initialise the variable with the address:
buffer := bytes.Buffer{}
fmt.Fprint(&buffer, "foo")
// or
bufferAddress = &bytes.Buffer{}
fmt.Fprint(bufferAddress, "foo")
a single struct may implement multiple interfaces
a simple mock can be implemented with a struct that mutates an internal property each time the interface it implemnts is called:
type MySpy struct {
Calls []string
}
// create a mock for time.Sleep
func (m *MySpy) Sleep(duration time.Duration) {
m.Calls = append(m.Calls, "called")
}
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
Research
Security News
Malicious npm package postcss-optimizer delivers BeaverTail malware, targeting developer systems; similarities to past campaigns suggest a North Korean connection.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.