转到1.9.2版,请参见以下代码:
package main
import (
"fmt"
"encoding/xml")
type DeviceId struct {
XMLName xml.Name `xml:"DeviceId"`
Manufacturer string `xml:"Manufacturer"`
OUI string `xml:"OUI"`
ProductClass string `xml:"ProductClass"`
SerialNumber string `xml:"SerialNumber"`
}
type CwmpInform struct {
XMLName xml.Name `xml:"cwmp:Inform"`
DeviceId DeviceId
}
func main() {
rq := new(CwmpInform)
data := `<cwmp:Inform>
<DeviceId>
<Manufacturer></Manufacturer>
<OUI>48BF74</OUI>
<ProductClass>FAP</ProductClass>
<SerialNumber>1202000042177AP0008</SerialNumber>
</DeviceId>
</cwmp:Inform>`
xml.Unmarshal([]byte (data), rq)
fmt.Printf("Unmarshelled Content:%v", rq)
output, err := xml.MarshalIndent(rq," ", " ")
if err != nil{
fmt.Printf("error : %v", err)
}
fmt.Printf("Marshelled Content: %s\n", string(output)) …Run Code Online (Sandbox Code Playgroud) 这是结构体及其方法的示例代码
type A struct {}
func (a *A) perfom(string){
...
...
..
}
Run Code Online (Sandbox Code Playgroud)
invoke()然后我想从包外部的函数调用该方法,示例代码
var s := A{}
func invoke(url string){
out := s.perfom(url)
...
...
}
Run Code Online (Sandbox Code Playgroud)
invoke我想通过模拟performA的方法来编写该函数的测试用例。
在java中,我们有mockito、jmock框架来存根方法调用。
go中有没有办法,我们可以模拟结构体的方法调用,而无需interfaces在源代码中引入?
调试目的需要这种输出.每次需要迭代时,要获得指针切片的实际值.
有没有办法,我们可以使用简单的fmt.printf()直接获取值而不是切片上每个项目的地址?
这是一个代码片段:https: //play.golang.org/p/bQ5vWTlKZmV
package main
import (
"fmt"
)
type user struct {
userID int
name string
email string
}
func main() {
var users []*user
addUsers(users)
}
func addUsers(users []*user) {
users = append(users, &user{userID: 1, name: "cooluser1", email: "cool.user1@gmail.com"})
users = append(users, &user{userID: 2, name: "cooluser2", email: "cool.user2@gmail.com"})
printUsers(users)
printEachUser(users)
}
func printUsers(users []*user) {
fmt.Printf("users at slice %v \n", users)
}
func printEachUser(users []*user) {
for index, u := range users {
fmt.Printf("user at …Run Code Online (Sandbox Code Playgroud) 有没有任何本地库或第三方支持,例如ScheduledExecutorServicego lang用于生产用例的java本机库?
请在java 1.8中找到代码片段:
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class TaskScheduler {
/**
* @param args
*/
public static void main(String[] args) {
Runnable runnable = ()-> {
// task to run goes here
System.out.println("Hello !!");
};
ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
service.scheduleAtFixedRate(runnable, 0, 1, TimeUnit.SECONDS);
}
}
Run Code Online (Sandbox Code Playgroud)
它将Hello !!每秒打印一次.
我通过使用发现了一些实现ScheduledExecutorService,但对生产用例不满意.
从源头看:
// Listen creates a TLS listener accepting connections on the
// given network address using net.Listen.
// The configuration config must be non-nil and must include
// at least one certificate or else set GetCertificate.
func Listen(network, laddr string, config *Config) (net.Listener, error) {
if config == nil || (len(config.Certificates) == 0 && config.GetCertificate == nil) {
return nil, errors.New("tls: neither Certificates nor GetCertificate set in Config")
}
l, err := net.Listen(network, laddr)
if err != nil {
return …Run Code Online (Sandbox Code Playgroud) 我想在运行go 测试和覆盖率时跳过包下的特定 go 文件。请找到以下命令,我曾经在我的服务包下运行:
cd src/service && go test --coverprofile coverageService.out --coverpkg ./.
Run Code Online (Sandbox Code Playgroud) 我已使用此链接在 Kubernetes 上部署 Postgres。
下面是配置图:
apiVersion: v1
kind: ConfigMap
metadata:
name: postgres-config
labels:
app: postgres
data:
POSTGRES_DB: postgresdb
POSTGRES_USER: postgresadmin
POSTGRES_PASSWORD: admin123
Run Code Online (Sandbox Code Playgroud)
和部署:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: postgres
spec:
replicas: 1
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: postgres:10.4
imagePullPolicy: "IfNotPresent"
ports:
- containerPort: 5432
envFrom:
- configMapRef:
name: postgres-config
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: postgredb
volumes:
- name: postgredb
persistentVolumeClaim:
claimName: postgres-pv-claim
Run Code Online (Sandbox Code Playgroud)
我希望 configmap 或部署 yaml 中的简单配置将有助于在 postgres 数据库上创建新模式。
go ×6
unit-testing ×2
kubernetes ×1
mocking ×1
postgresql ×1
scheduler ×1
slice ×1
ssl ×1
testing ×1
xml ×1