In this article we’ll discuss about the similarities and differences between Go and C’s struct.
In Go and C, a struct is a composite data type that allows us to group together related values.
Here are some similarities between Go’s struct and C’s struct :
- Syntax : The syntax for defining a struct in Go and C is similar.
For example :
// Syntax for defining a struct in C
struct Point {
int x;
int y;
};
// Syntax for defining a struct in Go
type Point struct {
X int
Y int
}
2. Fields :
Both Go’s and C’s struct can have fields (i.e., variables) of different types, and these fields can be accessed using dot notation.
For example :
// Accessing fields in a struct in C
struct Point p = {1, 2};
printf("%d %d\n", p.x, p.y);
// Accessing fields in a struct in Go
p := Point{X: 1, Y: 2}
fmt.Println(p.X, p.Y)
3. Pointers :
Both Go’s and C’s struct can be passed around as pointers, which allows us to modify the fields of the struct in a function without having to pass the entire struct by value.
For example :
// Using pointers with structs in C
void scale_by(struct Point *p, int factor) {
p->x *= factor;
p->y *= factor;
}
// Using pointers with structs in Go
func (p *Point) ScaleBy(factor int) {
p.X *= factor
p.Y *= factor
}
4. Anonymous fields :
Both Go’s and C’s struct can have anonymous fields, which are fields that do not have a name. Anonymous fields in Go are used to embed one struct within another, while anonymous fields in C are used to specify a type without creating a new name.
For example:
// Anonymous fields in C
struct Circle {
struct Point p;
int radius;
};
// Anonymous fields in Go
type Circle struct {
Point
Radius int
}
These are the similarities between Go and C’s struct. Now let’s talk about some differences between them.
Differences
- Typedef :
In C, we can use the typedef keyword to give a struct a new name, which can make it easier to use. In Go, we can use type aliases to give a struct a new name, but this is not the same as typedef.
2. Methods :
Go’s struct can have methods (i.e., functions that are associated with a struct), while C’s struct cannot. In C, we can define functions that take a struct as an argument, but these functions are not associated with the struct in the same way as methods.
3. Initialization :
In C, we can initialize a struct using a syntax similar to an array initialization.
In Go, we must use a field-by-field initialization, similar to an object initialization in JavaScript.
For example:
// Initializing a struct in C
struct Point p1 = {1, 2};
struct Point p2 = {.y = 3, .x = 4};
// Initializing a struct in Go
p1 := Point{X: 1, Y: 2}
p2 := Point{Y: 3, X: 4}
4. Arrays :
In C, we can define arrays of structs, while in Go, we must use a slice (i.e, a dynamically-sized array ) if we want to group structs together.
5. OOP :
C does not have native support for object-oriented programming (OOP), while Go does show some OOP features ( note : Go is not a fully OOP language ). This means that Go’s struct has more features than C’s struct.
Hope this clarified your confusion on Go and C’s struct.
Thanks for reading, see you in the next article :)
Reach me on 📱-
https://www.linkedin.com/in/saeeed/
https://twitter.com/saeed0x1