Skip to content

Instantly share code, notes, and snippets.

View sagar23sj's full-sized avatar
🎯
Focusing

Sagar Sonwane sagar23sj

🎯
Focusing
View GitHub Profile
@sagar23sj
sagar23sj / error.go
Created September 7, 2021 16:00
example of using error packeg in go
package main
import (
"errors"
"fmt"
)
func main() {
err := errors.New("it's an error")
package main
import (
"fmt"
)
// The error built-in interface type is the conventional interface
// for representing an error condition, with the nil value
// representing no error.
@sagar23sj
sagar23sj / errors.go
Created September 7, 2021 14:19
Contents of errors.go file from golang errors package
package errors
// New returns an error that formats as the given text.
// Each call to New returns a distinct error value even if the text is identical.
func New(text string) error {
return &errorString{text}
}
// errorString is a trivial implementation of error.
type errorString struct {
@sagar23sj
sagar23sj / method_overloading.go
Created September 5, 2021 09:58
Example showing failed attempt to perform method overloading in Golang
package main
import "fmt"
//Area function to calculate Area of Rectangle
func Area(length int, breadth int) int {
return length * breadth
}
//Area function to calculate Area of Square
@sagar23sj
sagar23sj / updated_main.go
Created August 30, 2021 12:42
Updated main.go for unexported embedded struct
package main
import (
"fmt"
"github.com/sagar23sj/Go-OOP/Encapsulation/animal"
)
func main() {
//Creating an object of type Dog from animal package
@sagar23sj
sagar23sj / unexported_animal.go
Created August 30, 2021 12:35
Unexported animal type in animal package
package animal
//Unexported type struct
type animal struct {
Name string
}
//Exported type Dog embedding type Animal
type Dog struct {
animal
@sagar23sj
sagar23sj / exported_1_animal.go
Last active August 30, 2021 12:23
Example - exported type Dog with embedded struct
package animal
//Exported type struct
type Animal struct {
Name string
}
//Exported type Dog embedding type Animal
type Dog struct {
Animal
@sagar23sj
sagar23sj / exported_method_employee.go
Last active August 30, 2021 11:55
Example - unexported field in struct
package employee
//Exported struct type Employee with employee details
type Employee struct {
Name string
Age int
salary float64
}
func (e *Employee) SetSalary(val float64) {
@sagar23sj
sagar23sj / unexported_field_1_employee.go
Last active August 30, 2021 11:23
Example - unexported field in struct
package employee
//Exported struct type Employee with employee details
type Employee struct {
Name string
Age int
salary float64
}
@sagar23sj
sagar23sj / exported_2_counter.go
Last active August 30, 2021 09:07
Example - Exported function for Unexported type
package counter
// internalCounter is an unexported identifier of integer type
type internalCounter int
// NewInternalCounter creates an object of internalCounter
// with value passed to it and returns
// an object of type internalCounter
func NewInternalCounter(val int) internalCounter {
return internalCounter(val)