Skip to content

Instantly share code, notes, and snippets.

@dacr
Created March 28, 2025 09:01
Show Gist options
  • Save dacr/d5fbf0f0632b5d7c37349f534fae447e to your computer and use it in GitHub Desktop.
Save dacr/d5fbf0f0632b5d7c37349f534fae447e to your computer and use it in GitHub Desktop.
go structures, methods and tags / published by https://github.com/dacr/code-examples-manager #b9b7e1b1-41c0-4ae5-a3d5-7b29d251a040/b1c393b9ea8519c98236dfa28eeeb956e36439c1
/*?sr/bin/true; exec /usr/bin/env nix-shell -p go --run "go run $0" #*/
// summary : go structures, methods and tags
// keywords : go, structures, methods, tags, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : b9b7e1b1-41c0-4ae5-a3d5-7b29d251a040
// created-on : 2025-03-27T11:10:41+01:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : nix-shell -p go --run "go run $file"
package main
import (
"encoding/json"
"encoding/xml"
"fmt"
"time"
)
type GenderType string
const (
Male GenderType = "male"
Female GenderType = "female"
)
// ======================================================================================================
type Person struct {
FirstName string `json:"first_name" xml:"firstName"`
LastName string `json:"last_name" xml:"lastName"`
Age int `json:"age" xml:"age"`
Gender GenderType `json:"gender" xml:"gender"`
BirthDate time.Time `json:"birth_date,omitempty" xml:"birthDate,omitempty" format:"iso8601"`
}
// default toString method !
func (me Person) String() string {
return fmt.Sprintf("%s %s (%d)", me.FirstName, me.LastName, me.Age)
}
func (me Person) IsMale() bool { // travail sur une copie d'une personne
return me.Gender == Male
}
func (me *Person) SetAge(age int) { // travail sur une référence, donc la mise à jour est effective
me.Age = age // du coup ne pas oublier la syntaxe pointeur
}
func (me Person) SetAgeBAD(age int) {
me.Age = age // mise à jour sur la copie !! Donc pas de mise à jour de la structure originale
}
// ======================================================================================================
func johnDoe1() Person {
someone := Person{}
someone.FirstName = "John"
someone.LastName = "Doe"
someone.Age = 30
someone.Gender = Male
someone.BirthDate = time.Now()
return someone
}
func johnDoe2() Person {
someone := Person{
FirstName: "John",
LastName: "Doe",
Age: 30,
Gender: Male,
BirthDate: time.Now(),
}
return someone
}
func main() {
john := johnDoe2()
john.SetAge(42)
fmt.Println(john)
johnJSON, err := json.MarshalIndent(john, "", " ")
if err != nil {
fmt.Println("Error marshalling john to JSON:", err)
return
}
fmt.Println(string(johnJSON))
johnXML, err := xml.MarshalIndent(john, "", " ")
if err != nil {
fmt.Println("Error marshalling john to XML:", err)
return
}
fmt.Println(string(johnXML))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment