有没有办法让运行时获得当前包的名称?
package main
import "fmt"
func main() {
pkgName := {some magic here:)}
fmt.Println(pkgName)
}
Run Code Online (Sandbox Code Playgroud)
......结果应该是"主要的"
现在我正在使用常数:
package main
import "fmt"
const (
pkgName = "main"
)
func main() {
fmt.Println(pkgName)
}
Run Code Online (Sandbox Code Playgroud)
但我很好奇你是否可以避免这种情况
从不同的goroutines访问不同的struct成员是否安全?
我明白,在没有同步的情况下写入同一个变量是很糟糕的:
package main
type Apple struct {
color string
size uint
}
func main() {
apple := &Apple{}
go func() {
apple.color = "red"
}()
go func() {
apple.color = "green"
}()
}
Run Code Online (Sandbox Code Playgroud)
但是你可以在没有任何同步的情况下写入不同的struct成员吗?
package main
type Apple struct {
color string
size uint
}
func main() {
apple := &Apple{}
go func() {
apple.color = "red"
}()
go func() {
apple.size = 42
}()
}
Run Code Online (Sandbox Code Playgroud)
或者我应该使用chan或sync.Mutex为此?
你可以在回调中使用$ this来获取phpunit中被模拟类的受保护属性吗?或者还有其他方法可以实现吗?
$mock = $this->getMock('A', array('foo'));
$mock->expects($this->any())->method('foo')->will(
$this->returnCallback(function() {
return $this->bar;
}));
Run Code Online (Sandbox Code Playgroud)
如果考虑注入模拟对象,这可能非常有用.有时类对其他类具有硬编码依赖性,但它使用方法创建它,理论上可以模拟和创建模拟对象而不是硬编码对象.请看另一个例子.
class A {
protected $bar = "bar";
public function foo () {
$b = new B();
return $b->fizz($this->bar);
}
}
class B {
public function fizz ($buzz) {
return $buzz;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,让我们说B级做坏事,我想用mock替换它.
$mockB = $this->getMock('B');
// (...) - and probably mock other things
$mockA = $this->getMock('A', array('foo'));
$mockA->expects($this->any())->method('foo')->will(
$this->returnCallback(function() use ($mockB) {
return $mockB->fizz($this->bar);
}));
Run Code Online (Sandbox Code Playgroud)
这在某种程度上是可以实现的吗?
当然没有任何意外,目前,如果我这样做,那么我得到错误:
PHP Fatal error: Using $this when not in object …Run Code Online (Sandbox Code Playgroud) 在php中有几种方法可以执行shell命令:
前两个显示输出但不返回.最后两个返回输出但不显示它.
我想运行shell命令,这需要很多时间,但它会显示一些输出,所以我知道它不会挂起.但最后我想在php中处理这个输出.如果我选择前两个中的一个,我将无法获得输出,因此我将无法在php中处理它.如果我运行最后两个中的一个,我将能够处理输出但是我的程序将挂起很长时间而不输出任何东西.
有没有办法运行一个shell命令,它会立即显示输出并返回它?
代码将解释一切:
<?php
class ATest extends PHPUnit_Framework_TestCase
{
public function testDestructorOnOriginalClass() {
$a = new A(); // It
unset($a); // works
echo " great!"; // great!
$this->expectOutputString('It works great!');
}
public function testDestructorOnMockedClass() {
$a = $this->getMock('A', array('someNonExistingMethod')); // It
unset($a); // works
echo " great!"; // great!
$this->expectOutputString('It works great!');
}
}
class A {
public function __construct()
{
echo "It";
}
public function __destruct()
{
echo " works";
}
}
Run Code Online (Sandbox Code Playgroud)
和输出:
# phpunit ATest.php
PHPUnit 3.7.13 by Sebastian Bergmann. …Run Code Online (Sandbox Code Playgroud) 什么是正确/首选的 REST api 设计来获取取决于时区的数据?
我正在考虑简单地将时区添加为 URL 参数,因此:
$ curl www.api.example.com/data?timezone="Europe/Amsterdam"
Run Code Online (Sandbox Code Playgroud)
但是有人建议以类似于 github 的方式使用自定义标头:
$ curl -H "X-Time-Zone: Europe/Amsterdam" www.api.example.com/data
Run Code Online (Sandbox Code Playgroud)
https://developer.github.com/v3/#using-the-time-zone-header
我想知道在这里使用 header 而不是 param 有什么好处?这样的决定背后是否有任何设计模式?
php ×3
go ×2
mocking ×2
phpunit ×2
unit-testing ×2
destructor ×1
http-headers ×1
mutex ×1
rest ×1
shell ×1
sync ×1
timezone ×1