Golang package test coverage not working in version 1.10.1 - go

I'm trying to generate test coverage for a package using the following commands.
go test -v ./models_test -coverpkg=./models -coverprofile=c.out && go tool cover -func=c.out
go test -v ./models_test -coverpkg=github.com/apremalal/go-test-cov/models -coverprofile=c.out && go tool cover -func=c.out
My repo structure is like below, and I'm executing commands indie folder
<go-test-cov>
|
-<models>
|
-<models-test>
I have hosted the code in github for easy referencing.
models/math.go
package models
func Add(x,y int) int{
return x+y
}
models_test/math_test.go
package models_test
import (
"testing"
"github.com/apremalal/go-test-cov/models"
)
func TestAdd(t *testing.T) {
total := models.Add(5, 5)
if total != 10 {
t.Errorf("Sum was incorrect, got: %d, want: %d.", total, 10)
}
}
Each time I receive 0% test coverage though the tests get executed without errors.
I'm using go version go1.10.1 darwin/amd64. This method used to work in earlier go versions. Appreciate your help in finding out how to get this working with the latest go release.

Related

VSCode import problem with windows API call

When importing golang.org/x/sys/windows in VSCode, I can only choose SIGDescribe, nothing else.
Hovering over the import, following errors appear.
error while importing golang.org/x/sys/windows: build constraints exclude all Go files in /home/username/go/pkg/mod/golang.org/x/sys#v0.0.0-20210630005230-0f9fa26af87c/windows
could not import golang.org/x/sys/windows (no required module provides package "golang.org/x/sys/windows")compilerBrokenImport
The manual command go get golang.org/x/sys/windows gives the following error message
Command 'gopls.go_get_package' failed: Error: err: exit status 1: stderr: package golang.org/x/sys/windows: build constraints exclude all Go files in /home/username/go/pkg/mod/golang.org/x/sys#v0.0.0-20210630005230-0f9fa26af87c/windows .
I already re-installed Golang and updated GoTools in VSCode, no changes.
Goal: The following code below should work.
package main
import "golang.org/x/sys/windows"
func main() {
user32DLL := windows.NewLazyDLL("user32.dll")
}
OS: Ubuntu 21.04
GO Version: 1.16.6
Editor: VSCode 1.58.1
Make a folder somewhere something. Then make a file something/main.go:
package main
import "golang.org/x/sys/windows"
func main() {
println(windows.EVENTLOG_ERROR_TYPE == 1)
}
Then build:
go mod init something
go mod tidy
go build

Golang: using Windows 10 API / UWP / System.WindowsRuntime?

Using syscall in Go how can I call the UWP APIs within Windows 10? I have seen and tried many win32 examples, but when I tried using System.WindowsRuntime.dll it was a no-go; specifically, I received:
panic: Failed to load System.WindowsRuntime.dll: The specified module could not be found.
(this was at runtime, the binary built fine)
I tried building both with a standard go build as well as
go build -ldflags="-H windows"
example code:
var(
windowsRuntime = syscall.NewLazyDLL("System.WindowsRuntime.dll")
getDiskFreeSpace = windowsRuntime.NewProc("GetDiskFreeSpace")
)
Note: Other variants tried:
windowsRuntime = syscall.NewLazyDLL("System.Runtime.WindowsRuntime.dll")
&
windowsRuntime = syscall.NewLazyDLL("WindowsRuntime.dll")
Anyone been able to get this running or have any advice on the matter?
As always, greatly appreciated!!
Create a file like this:
//go:generate mkwinsyscall -output zfree.go free.go
//sys getDiskFreeSpace(pathName string, sectorsPerCluster *int, bytesPerSector *int, freeClusters *int, numberOfClusters *int) (err error) = GetDiskFreeSpaceA
package main
func main() {
var bytesPerSector, freeClusters, numberOfClusters, sectorsPerCluster int
getDiskFreeSpace(
`C:\`,
&sectorsPerCluster,
&bytesPerSector,
&freeClusters,
&numberOfClusters,
)
println("bytesPerSector", bytesPerSector)
println("freeClusters", freeClusters)
println("numberOfClusters", numberOfClusters)
println("sectorsPerCluster", sectorsPerCluster)
}
Then build:
go generate
go mod init free
go mod tidy
go build
Result:
bytesPerSector 512
freeClusters 12511186
numberOfClusters 25434879
sectorsPerCluster 8
https://github.com/golang/sys/tree/master/windows/mkwinsyscall
I have no idea where you found "System.WindowsRuntime.dll" because as far as I can tell, it doesn't exist (unless you manually made a DLL named that).
As for System.Runtime.WindowsRuntime.dll: it's part of .NET Framework, not Windows, and it is a managed DLL. You cannot load such DLLs using syscall.NewLazyDLL. What's more, that DLL doesn't really contain any Windows APIs - just glue for .NET to be able to work with them.
You're probably looking for functions like RoGetActivationFactory in combase.dll.

Stringer tool complains about wrong archive header

I am trying to use go generate/stringer (golang.org/x/tools/cmd/stringer) to generate String() methods on enums. I have problems, which I believe, are because of slightly different format of .a packages on different systems. I have this file:
package main
import (
"math/rand"
)
//go:generate stringer -type=Foo
type Foo int;
const (
FooPrime Foo = iota
FooBis
)
func main() {
//Just use rand anywhere, otherwise we get a compiler error
rand.Seed(1)
}
Now if I run go generate example.go on my machine everything is all right: foo_string.go is created. However, on a test machine I get:
stringer: checking package: example.go:4:2: could not import math/rand (reading export data: /usr/lib64/go/pkg/linux_amd64/math/rand.a: go archive is missing __.PKGDEF)
Now, after some digging in the code I think that I get this error, because on my machine rand.a has the following header:
!<arch>
__.PKGDEF 0 0 0 644 2051
`
while on test machine it has the following header:
!<arch>
__.PKGDEF/ 0 399 399 100644 2051
`
I think that the crucial difference is slash after PKGDEFF. gcimporter refuses to process .a file, if it doesn't have __.PKGDEF header.
To check this, I edited manually gcimporter/exportdata.go and changed one of the line from this:
if name != "__.PKGDEF"
to this:
if name != "__.PKGDEF" && name != "__.PKGDEF\"
After this change (and compiling and installing everything) I was able to run go generate on example.go.
My questions are: why do I get this problem and how do I get rid of it (other then manually editing external library)?
What I can see from the spec for openSUSE's packaging they are disabling reinstallation of the standard library at updates. __.PKGDEF is a Go specific informational section, and some linker OpenSUSE has used has simply produced incompatible output.
There's nothing you can do except install a healthy Go from the official source.

Updating go websocket library to latest version

I am running the Go compiler on Ubuntu, installed using sudo apt-get install golang
I've successfully compiled and executed the code for a Trivial example server (See http://golang.org/pkg/websocket/#Handler )
package main
import (
"http"
"io"
"websocket"
)
// Echo the data received on the Web Socket.
func EchoServer(ws *websocket.Conn) {
io.Copy(ws, ws);
}
func main() {
http.Handle("/echo", websocket.Handler(EchoServer));
err := http.ListenAndServe(":12345", nil);
if err != nil {
panic("ListenAndServe: " + err.String())
}
}
However, I fail to connect to the server with my version of Chromium (16.0.912.77). I assume Chrome has implemented the RFC 6455 Websocket (version 13), but that the go websocket library in the Ubuntu golang package is out of date.
So, my question is: How can I update only the websocket package to the latest version?
The latest version of the Go websocket package is net/websocket at code.google.com/p/go.net/websocket, which requires the Go 1 weekly development release.
For Ubuntu golang-weekly: Ubuntu PPA packages for Go.
For weekly development release documentation: Go Programming Language.
I guess the version of Go in Ubuntu package repository is probably r60.3 (or so), which is a bit old now. Use latest weekly, change the code to:
package main
import (
"code.google.com/p/go.net/websocket"
"io"
"net/http"
)
// Echo the data received on the Web Socket.
func EchoServer(ws *websocket.Conn) {
io.Copy(ws, ws)
}
func main() {
http.Handle("/echo", websocket.Handler(EchoServer))
err := http.ListenAndServe(":12345", nil)
if err != nil {
panic("ListenAndServe: " + err.Error())
}
}
Moreover in the websocket package s/ParseRequestURI/ParseRequest/, then it seems to work here.(1)
Update: Sorry, I wrote/read too fast, it doesn't seem to work, the page shows: "not websocket protocol" (here is Chrome 18.0.1025.33 beta on 64b Ubuntu 10.04)
Update 2012-08-22: The above (1) note about editing the websocket package doesn't hold anymore. The websocket package has been meanwhile updated and the example (main) code above now compiles w/o problems. Anyway, I haven't tested if it afterwards does what is should or not, sorry.

How can I compile a Go program?

I got Go to compile:
0 known bugs; 0 unexpected bugs
and typed in the "hello world":
package main
import "fmt"
func main() {
fmt.Printf("Hello, 世界\n")
}
Then I tried to compile it, but it wouldn't go:
$ 8c gotest2
gotest2:1 not a function
gotest2:1 syntax error, last name: main
This is going on on Ubuntu Linux on Pentium. Go installed and passed its tests. So where did I go wrong? Can someone tell me where to go from here?
I also tried this program:
package main
import fmt "fmt" // Package implementing formatted I/O.
func main() {
fmt.Printf("Hello, world; or Καλημέρα κόσμε; or こんにちは 世界\n");
}
But this was also no go (must stop making go puns):
$ 8c gotest3.go
gotest3.go:1 not a function
gotest3.go:1 syntax error, last name: main
For Go 1.0+ the correct build command is now: go build
You're using 8c, which is the c compiler. 8g will compile go, and 8l will link.
(Update for Go1.0.x)
The section "Compile packages and dependencies" now list go build as the way to compile in go.
You still call 8g behind the scene, and the parameters you could pass to 8g are now passed with -gcflags.
-gcflags 'arg list'
arguments to pass on each 5g, 6g, or 8g compiler invocation
use go run to run the go program. Here is the output.
$ cat testgo.go
package main
import "fmt"
func main() {
fmt.Printf("Hello, 世界\n")
}
$go run testgo.go
Hello, 世界
To compile Go code, use the following commands:
go tool compile gotest3.go # To create an object file.
go tool link -o gotest3 gotest3.o # To compile from the object file.
chmod +x gotest3 # To apply executable flag.
./gotest3 # To run the binary.

Resources