Tag Archives: golang

Go test print no output

has a test file:

$ cat utils/utils_test.go
package utils

import (
        "fmt"
        "testing"
)

func TestGetProjAbsPath(t *testing.T) {
        projPath := GetProjAbsPath("github.com", "GerryLon", "go-crawler")
        t.Log(projPath)
        fmt.Println("projPath is:", projPath)
}

runs as follows:

$ go test -count=1 utils/*.go
ok      command-line-arguments  0.002s

does not see print, neither t.og nor FMT.Println.

Add the

plus -v(verbose) option to see the whole process :

$ go test -v  utils/*.go
=== RUN   TestGetProjAbsPath
projPath is: /var/workspace/go/src/github.com/GerryLon/go-crawler
--- PASS: TestGetProjAbsPath (0.00s)
    utils_test.go:10: /var/workspace/go/src/github.com/GerryLon/go-crawler
PASS
ok      command-line-arguments  (cached)

Reference:


https://stackoverflow.com/questions/23205419/how-do-you-print-in-a-go-test-using-the-testing-package

welcome to add correction!

Beego method to find memory leak get memprof

turn on debugging function, modify configuration file app.conf:

EnableAdmin = true
AdminAddr = "0.0.0.0"
AdminPort = 8088

access management page: http://your IP :8088/

click on the menu: get memprof, prompt create heap profile mem-948. Memprof means memory information file was successfully generated

execute go tool pprof your_app mem – 948. Memprof, SVG command input, if prompt dot not installed, in https://graphviz.org/download/ download graphviz, execute right can generate profile001. SVG file, use the browser open, where how much memory consumption of the be clear at a glance.

if you click on the menu get cpuprof, you can view the CPU stack information.

Example of public key signature and verification generated with fabric case

public and private keys generated by Fabric case

The

Fabric case generates public and private key pairs for nodes such as Peer, User, Admin, etc. Public and private keys are shown in the figure below.


example of signing and validating with public and private key pairs

Example code for signing and validating a message with the above public and private key pairs is shown below.

package main

import (
	"crypto/ecdsa"
	"crypto/rand"
	"crypto/sha256"
	"crypto/x509"
	"encoding/pem"
	"fmt"
	"io/ioutil"
)

func main() {
	msg := "hello, world"
	hash := sha256.Sum256([]byte(msg))
	msg2 := "Hello, world"
	hash2 := sha256.Sum256([]byte(msg2))

	privBytes, _ := ioutil.ReadFile("./priv_sk")
	blkPriv, _ := pem.Decode(privBytes)
	fmt.Println("priv_sk  type:", blkPriv.Type)
	key, _ := x509.ParsePKCS8PrivateKey(blkPriv.Bytes)
	ecdsaKey := key.(*ecdsa.PrivateKey)
	r, s, _ := ecdsa.Sign(rand.Reader, ecdsaKey, hash[:])

	certBytes, _ := ioutil.ReadFile("./cert.pem")
	blkCert, _ := pem.Decode(certBytes)
	fmt.Println("cert.pem type:", blkCert.Type)
	cert, _ := x509.ParseCertificate(blkCert.Bytes)
	pubkey := cert.PublicKey.(*ecdsa.PublicKey)
	ok := ecdsa.Verify(pubkey, hash[:], r, s)
	fmt.Println("verify hash(shoule be true):", ok)

	ok = ecdsa.Verify(pubkey, hash2[:], r, s)
	fmt.Println("verify hash2(shoule be false):", ok)
}

compiles and runs as shown below.

How to compare the equality of structures in golang

if the object is created by the same struct without complex type can be directly used by == on the ratio and pointer

Simple type

sortable data type

Integer
Floating point
String

data type that can be compared

in addition to the above three, there are
Boolean,
Complex,
Pointer,
Channel,
Interface
Array

complex non-comparable data type

Slice
Map
Function

as follows

type User struct {
	age  int
	name string
}

func main() {
	user := User{
		1,
		"d",
	}
	user2 := User{
		1,
		"d",
	}
	fmt.Println(user == user2)  //打印的结果是true 会去自动对比内部的属性是否相等
	//但是如果结构体内部含有map,slice,Function 使用==比较编译会报错
}

the == operator
example

can be used if two objects that are not created in the same structure can be converted to each other and do not contain non-comparable member variables

type USER struct {
	age  int
	name string
	u    Name
}
type Name struct {
	a string
}
type USER2 struct {
	age  int
	name string
	u    Name
}

func main() {
	user := USER{
		1,
		"d",
		Name{""},
	}
	user3 := USER2{
		1,
		"d",
		Name{""},
	}
	user2 := USER(user3)
	fmt.Println(user2 == user) //编译通过 打印结果是true
}

refer to official document