Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Singleton Pattern 馃帀 #3

wants to merge 1 commit into
base: master
Changes from all commits
File filter...
Filter file types
Jump to鈥
Jump to file or symbol
Failed to load files and symbols.


Just for now

@@ -0,0 +1,27 @@
package patterns

// HTTPRequest struct
type HTTPRequest struct {
count int

var httpService *HTTPRequest

// GetHTTPRequestService function returns a HTTPRequest
func GetHTTPRequestService() *HTTPRequest {
if httpService == nil {
httpService = new(HTTPRequest)

return httpService

// MakeRequest method on HTTPRequest struct
func (r *HTTPRequest) MakeRequest() {

// GetNumberOfRequests method on HTTPRequest struct
func (r *HTTPRequest) GetNumberOfRequests() int {
return r.count
@@ -0,0 +1,27 @@
package patterns

import "testing"

func TestGetHTTPRequestService(t *testing.T) {
firstRequest := GetHTTPRequestService()
if firstRequest == nil {
t.Error("A new connection must have been made")
expectedCounter := firstRequest


if currentCount := firstRequest.GetNumberOfRequests(); currentCount != 1 {
t.Errorf("After calling http service for the first time, the count must be 1 but it is %d\n", currentCount)

secondRequest := GetHTTPRequestService()
if secondRequest != expectedCounter {
t.Error("Singleton instances must be different")

if currentCount := secondRequest.GetNumberOfRequests(); currentCount != 2 {
t.Errorf("After calling http service for the second time using different request instance, the current count must be 2 but was %d\n", currentCount)
ProTip! Use n and p to navigate between commits in a pull request.
You can鈥檛 perform that action at this time.