SlideShare a Scribd company logo
1 of 30
Download to read offline
REFACTORING A GOREFACTORING A GO
PROJECTPROJECT
DAN TRANDAN TRAN
Created: 2019-02-05 Tue 23:27
1
ABOUT THIS PROJECTABOUT THIS PROJECT
2 . 1
EVERORGEVERORG
Original repo:
Fork repo:
https://github.com/mgmart/EverOrg
https://github.com/aladine/EverOrg
2 . 2
HOW IT WORKSHOW IT WORKS
2 . 3
WHAT TO REFACTORWHAT TO REFACTOR
Statically checking Go source for errors and warning
Add benchmark test to improve performance
Add feature to split notebook to individual les
3 . 1
GO LINTER REPORTGO LINTER REPORT
4 . 1
GO REPORTGO REPORT
Link:
gofmt: linter
govet: examines Go source code and reports
suspicious constructs
gocyclo: check code complexity
golint: linting syntax code
ineffassign: detects ineffectual assignments in Go
code.
license
misspell
https://goreportcard.com
4 . 2
GOMETALINTERGOMETALINTER
go get gopkg.in/alecthomas/gometalinter.v1
gometalinter.v1 --disable=gocyclo --disable=vetshadow --disable=g
--disable=gotype --disable=errcheck --vendor --disable=golint 
--disable=interfacer --json ./...
4 . 3
GOLANGCI LINTERGOLANGCI LINTER
go get -u github.com/golangci/golangci-lint/cmd/golangci-lint
GO111MODULE=on GOCACHE=on golangci-lint run
DEMO: GOLANGCI LINTERDEMO: GOLANGCI LINTER
4 . 5
STATIC ANALYSIS TOOLSSTATIC ANALYSIS TOOLS
4 . 6
GOFMTGOFMT
go fmt < gofmt < goimports
package main
import (
"fmt"
)
func main() {
s := []string{}
var m map[string]interface{}
s = append(s, "a")
s = append(s, "b")
for i, _ := range s {
fmt.Println(i)
m[s[i]] = i
}
}
4 . 7
A (maybe) better version
package main
import (
"fmt"
)
func main() {
var s []string
m := map[string]interface{}{}
s = append(s, "a")
s = append(s, "b")
for i := range s {
fmt.Println(i)
m[s[i]] = i
}
}
4 . 8
GORENAMEGORENAME
A precise, type-safe identi er renaming
4 . 9
ALIGNCHECKALIGNCHECK
4 . 10
// Note ...
type Note struct {
Title string `xml:"title"`
Content string `xml:"content"`
HasAttachments bool `xml:"has_attachments"`
Created string `xml:"created"`
IsDeleted bool `xml:"is_deleted"`
}
http://golang-sizeof.tips/
4 . 11
A better version
type Note struct {
Title string `xml:"title"`
Content string `xml:"content"`
Created string `xml:"created"`
HasAttachments bool `xml:"has_attachments"`
IsDeleted bool `xml:"is_deleted"`
}
4 . 12
GOCYCLOGOCYCLO
Calculates cyclomatic complexities of functions in Go
source code
The count +1 for each 'if', 'for', 'case', '&&' or '||'
It gives warning if total > 15
4 . 13
GO REPORT BEFORE & AFTERGO REPORT BEFORE & AFTER
4 . 14
BEFOREBEFORE
AFTERAFTER
TESTINGTESTING
5 . 1
TEST COVERAGETEST COVERAGE
5 . 2
go test -covermode=count -coverprofile=count.out
go tool cover -func=count.out
go tool cover -html=count.out
5 . 3
BENCHMARKBENCHMARK
5 . 4
go test -bench=. -benchtime=30s
goos: darwin
goarch: amd64
pkg: github.com/aladine/EverOrg
BenchmarkOrgFormatOriginal-12 100 44262624
BenchmarkOrgConcatStringBuffer-12 20000 213045
BenchmarkOrgConcatStringBuilder-12 30000 163609
PASS
ok github.com/aladine/EverOrg 180.019s
5 . 5
DEMO: GO BENCHMARKDEMO: GO BENCHMARK
5 . 6
OTHER POSSIBLE REFACTORINGOTHER POSSIBLE REFACTORING
Use interface more often, especially io.Reader and
io.Writer
Add more test to increase test coverage
5 . 7
Go Releaser
before:
hooks:
builds:
- env:
- CGO_ENABLED=0
- goos:
- darwin
- linux
- windows
- ignore:
- goos: darwin
goarch: 386
archive:
name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{
replacements:
darwin: Darwin
5 . 8
REFERENCEREFERENCE
Images:
http://golang-sizeof.tips/
https://dave.cheney.net/2015/10/09/padding-is-
hard
https://goreportcard.com
https://github.com/aladine/EverOrg
https://blog.golang.org/cover
https://github.com/alecthomas/gometalinter
https://github.com/golangci/golangci-lint
https://unsplash.com
6 . 1

More Related Content

What's hot

Introduction to cdi given at java one 2014
Introduction to cdi given at java one 2014Introduction to cdi given at java one 2014
Introduction to cdi given at java one 2014
Antoine Sabot-Durand
 
Improving DroidBox
Improving DroidBoxImproving DroidBox
Improving DroidBox
Kelwin Yang
 
10reasons
10reasons10reasons
10reasons
Li Huan
 

What's hot (20)

What do you mean it needs to be Java based? How jython saved the day.
What do you mean it needs to be Java based? How jython saved the day.What do you mean it needs to be Java based? How jython saved the day.
What do you mean it needs to be Java based? How jython saved the day.
 
CDI In Real Life
CDI In Real LifeCDI In Real Life
CDI In Real Life
 
How to build a tool for operating Flink on Kubernetes
How to build a tool for operating Flink on KubernetesHow to build a tool for operating Flink on Kubernetes
How to build a tool for operating Flink on Kubernetes
 
Communication between Java and Python
Communication between Java and PythonCommunication between Java and Python
Communication between Java and Python
 
Droidcon Online 2020 quick summary
Droidcon Online 2020 quick summaryDroidcon Online 2020 quick summary
Droidcon Online 2020 quick summary
 
Introduction to cdi given at java one 2014
Introduction to cdi given at java one 2014Introduction to cdi given at java one 2014
Introduction to cdi given at java one 2014
 
JPA Week3 Entity Mapping / Hexagonal Architecture
JPA Week3 Entity Mapping / Hexagonal ArchitectureJPA Week3 Entity Mapping / Hexagonal Architecture
JPA Week3 Entity Mapping / Hexagonal Architecture
 
Knowledge Sharing Session on JavaScript Source Maps & Angular Compilation
Knowledge Sharing Session on JavaScript Source Maps & Angular CompilationKnowledge Sharing Session on JavaScript Source Maps & Angular Compilation
Knowledge Sharing Session on JavaScript Source Maps & Angular Compilation
 
Developing Cross Platform Applications with Golang
Developing Cross Platform Applications with GolangDeveloping Cross Platform Applications with Golang
Developing Cross Platform Applications with Golang
 
Building Serverless applications with Python
Building Serverless applications with PythonBuilding Serverless applications with Python
Building Serverless applications with Python
 
Griffon Presentation
Griffon PresentationGriffon Presentation
Griffon Presentation
 
Android reverse engineering - Analyzing skype
Android reverse engineering - Analyzing skypeAndroid reverse engineering - Analyzing skype
Android reverse engineering - Analyzing skype
 
Who Needs Thumbs? Reverse Engineering Scramble With Friends
Who Needs Thumbs? Reverse Engineering Scramble With FriendsWho Needs Thumbs? Reverse Engineering Scramble With Friends
Who Needs Thumbs? Reverse Engineering Scramble With Friends
 
[INNOVATUBE] Tech Talk #3: Golang - Takaaki Mizuno
 [INNOVATUBE] Tech Talk #3: Golang - Takaaki Mizuno [INNOVATUBE] Tech Talk #3: Golang - Takaaki Mizuno
[INNOVATUBE] Tech Talk #3: Golang - Takaaki Mizuno
 
Improving DroidBox
Improving DroidBoxImproving DroidBox
Improving DroidBox
 
10reasons
10reasons10reasons
10reasons
 
Madrid JAM limitaciones - dificultades
Madrid JAM limitaciones - dificultadesMadrid JAM limitaciones - dificultades
Madrid JAM limitaciones - dificultades
 
Building Command Line Tools with Golang
Building Command Line Tools with GolangBuilding Command Line Tools with Golang
Building Command Line Tools with Golang
 
Mixing Python and Java
Mixing Python and JavaMixing Python and Java
Mixing Python and Java
 
Hacking for Fun and Profit
Hacking for Fun and ProfitHacking for Fun and Profit
Hacking for Fun and Profit
 

Similar to Refactoring a go project

Introduction to Dynamic Analysis of Android Application
Introduction to Dynamic Analysis of Android ApplicationIntroduction to Dynamic Analysis of Android Application
Introduction to Dynamic Analysis of Android Application
Kelwin Yang
 
Migrating Legacy Rails Apps to Rails 3
Migrating Legacy Rails Apps to Rails 3Migrating Legacy Rails Apps to Rails 3
Migrating Legacy Rails Apps to Rails 3
Clinton Dreisbach
 
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
Guillaume Laforge
 

Similar to Refactoring a go project (20)

JavaOne 2009 BOF-5189 Griffon In Depth
JavaOne 2009 BOF-5189 Griffon In DepthJavaOne 2009 BOF-5189 Griffon In Depth
JavaOne 2009 BOF-5189 Griffon In Depth
 
Go 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoGo 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX Go
 
Introduction to Dynamic Analysis of Android Application
Introduction to Dynamic Analysis of Android ApplicationIntroduction to Dynamic Analysis of Android Application
Introduction to Dynamic Analysis of Android Application
 
DWX 2022 - DevSecOps mit GitHub
DWX 2022 - DevSecOps mit GitHubDWX 2022 - DevSecOps mit GitHub
DWX 2022 - DevSecOps mit GitHub
 
[COSCUP 2021] A trip about how I contribute to LLVM
[COSCUP 2021] A trip about how I contribute to LLVM[COSCUP 2021] A trip about how I contribute to LLVM
[COSCUP 2021] A trip about how I contribute to LLVM
 
Grokking Techtalk #38: Escape Analysis in Go compiler
 Grokking Techtalk #38: Escape Analysis in Go compiler Grokking Techtalk #38: Escape Analysis in Go compiler
Grokking Techtalk #38: Escape Analysis in Go compiler
 
Import golang; struct microservice
Import golang; struct microserviceImport golang; struct microservice
Import golang; struct microservice
 
Advanced debugging  techniques in different environments
Advanced debugging  techniques in different environmentsAdvanced debugging  techniques in different environments
Advanced debugging  techniques in different environments
 
Django - 次の一歩 gumiStudy#3
Django - 次の一歩 gumiStudy#3Django - 次の一歩 gumiStudy#3
Django - 次の一歩 gumiStudy#3
 
gumiStudy#3 Django – 次の一歩
gumiStudy#3 Django – 次の一歩gumiStudy#3 Django – 次の一歩
gumiStudy#3 Django – 次の一歩
 
Behat internals for advanced usage. Symfony Camp 2016
Behat internals for advanced usage. Symfony Camp 2016Behat internals for advanced usage. Symfony Camp 2016
Behat internals for advanced usage. Symfony Camp 2016
 
Migrating Legacy Rails Apps to Rails 3
Migrating Legacy Rails Apps to Rails 3Migrating Legacy Rails Apps to Rails 3
Migrating Legacy Rails Apps to Rails 3
 
Security Tips to run Docker in Production
Security Tips to run Docker in ProductionSecurity Tips to run Docker in Production
Security Tips to run Docker in Production
 
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
 
Drone Continuous Integration
Drone Continuous IntegrationDrone Continuous Integration
Drone Continuous Integration
 
Taking Jenkins Pipeline to the Extreme
Taking Jenkins Pipeline to the ExtremeTaking Jenkins Pipeline to the Extreme
Taking Jenkins Pipeline to the Extreme
 
Optimizing Spring Boot apps for Docker
Optimizing Spring Boot apps for DockerOptimizing Spring Boot apps for Docker
Optimizing Spring Boot apps for Docker
 
F3X12 FLOW3 Project Lifecycle
F3X12 FLOW3 Project LifecycleF3X12 FLOW3 Project Lifecycle
F3X12 FLOW3 Project Lifecycle
 
What's New in Groovy 1.6?
What's New in Groovy 1.6?What's New in Groovy 1.6?
What's New in Groovy 1.6?
 
David Rey Lessons Learned Updating Content Licensing To Be Plone 3 Compat...
David Rey   Lessons Learned   Updating Content Licensing To Be Plone 3 Compat...David Rey   Lessons Learned   Updating Content Licensing To Be Plone 3 Compat...
David Rey Lessons Learned Updating Content Licensing To Be Plone 3 Compat...
 

Recently uploaded

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Recently uploaded (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 

Refactoring a go project