# Golang with sonarqube

**URL:** <https://community.sonarsource.com/t/golang-with-sonarqube/129297>\
**Category:** SonarQube Server / Community Build\
**Tags:** go, coverage\
**Created:** [October 29, 2024, 2:02am UTC](https://community.sonarsource.com/t/golang-with-sonarqube/129297 "2024-10-29T02:02:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lgy10271416](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/lgy10271416/32/63609_2.png) [@lgy10271416](https://community.sonarsource.com/u/lgy10271416)\
**Post date:** [October 29, 2024, 2:02am UTC](https://community.sonarsource.com/t/golang-with-sonarqube/129297/1 "2024-10-29T02:02:39Z")

</div>

I now want to use gitlab-ci to integrate the go project into sonarqube, but the test coverage and static check results displayed by sonar are very different from what I obtain locally.

```auto
sonarqube-check:
  stage: sonarqube-check
  image:
    name: registry.cnbita.com:5000/golangci/sonar-scanner-cli-go122:v1
    pull_policy: if-not-present
    entrypoint: [""]
  variables:
    SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar" # Defines the location of the analysis task cache
    GIT_DEPTH: "0" # Tells git to fetch all the branches of the project, required by the analysis task
    SONAR_HOST_URL: "${SONAR_HOST_URL}"
    SONAR_TOKEN: "${SONAR_TOKEN}"
    SONAR_PROJECT_KEY: "${SONAR_PROJECT_KEY}"
  allow_failure: true
  coverage: '/^total:\s*\(statements\)\s*([\d\.]+)%/'
  script:
# - bash ./sonar/sonarqube-check.sh
    - go mod vendor
    - mkdir -p sonar/reports
    - golangci-lint run --config=./.golangci.yml ./... --out-format checkstyle > sonar/reports/report.xml
    - go test ./... -json > sonar/reports/test-report.out
    - go test ./... -v -coverprofile=sonar/reports/coverage.out 2>&1 | go-junit-report > sonar/reports/test-report.xml
    - go tool cover -func="sonar/reports/coverage.out"
    - |
      sonar-scanner \
        -Dsonar.host.url=$SONAR_HOST_URL \
        -Dsonar.login=$SONAR_TOKEN \
        -Dsonar.projectKey=$SONAR_PROJECT_KEY \
        -Dsonar.branch.name=$CI_COMMIT_REF_NAME \
        -X

```

sonar-project.properties

```auto
sonar.projectKey=hero-os_volume-controller_AZLDEWwhAEg22spS8Gl1
sonar.sourceEncoding=UTF-8
sonar.sources=.
sonar.language=go
sonar.exclusions= **/vendor/** , **/tests/** , **/build/** , **/config/** , **/configs/** , **/deployments/** , **/docs/** , **/hack/** ,**/*_test.go
sonar.tests=.
sonar.test.inclusions=**/*_test.go
sonar.test.exclusions= **/vendor/**
sonar.go.tests.reportPaths=sonar/reports/test-report.out
sonar.go.coverage.reportPaths=sonar/reports/coverage.out
sonar.go.golangci-lint.reportPaths=sonar/reports/report.xml
sonar.qualitygate.wait=true

```

 ![image](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/6/2/620cf8ae3a927ecf950a73d79d0b707ab3d08285.png)  
The picture above shows the unit coverage displayed when gitlabci is executed.The following display shows sonarqube, whether it is unit test coverage or static inspection, it is different. I think sonarqube does not read the report information from the specified file.  
 ![image](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/4/3/438651db915b9bd1576a5357eb260afc98941428.png)

---

<div class="post-metadata">

**Author:** ![ganncamp](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/ganncamp/32/1261_2.png) [@ganncamp](https://community.sonarsource.com/u/ganncamp)\
**Post date:** [October 30, 2024, 12:37pm UTC](https://community.sonarsource.com/t/golang-with-sonarqube/129297/2 "2024-10-30T12:37:04Z")

</div>

Hi,

This [dusty-but-still-relevant blog post](https://www.sonarsource.com/blog/executable_lines/) may help.

If there are files that SonarQube is including in coverage calculations that should be left out, you’ll want to set [coverage exclusions](https://docs.sonarsource.com/sonarqube/latest/project-administration/analysis-scope/#code-coverage-exclusion).

&nbsp;  
HTH,  
Ann
