Похожие чаты

Hi When does we use pointer to something in return type

as signature instead of the type it self ?
func function () *T
Instead
Func function () T

What is the benefit ?

17 ответов

46 просмотров

T in this case is an copy of an type. Different goroutines can act on it without affecting the others. With pointers they access the same memory and when the wrize to that shared space it can lead to problems. Copies are rather expensive for bigger objects, so sometimes the originals are pooled and only accessed through pointers.

Mehrad- Автор вопроса
Sebastian
T in this case is an copy of an type. Different go...

what about method receivers ? can i always use pointer receiver instead of value receiver ? when should i use value receiver ?

Mehrad
what about method receivers ? can i always use poi...

Think of receivers as a first argument to your function. Non-pointer receivers will copy. Now depending on what you want to do — mutate or not, and how big the data structure is, you may prefer one over the other. Although the rule of thumb is, be consistent with your receivers, and when in doubt, use a pointer receiver.

Mehrad
what about method receivers ? can i always use poi...

https://go.dev/tour/methods/8 don 't forget however, that copying small stuff (usually < 64 bytes) is very cheap and pointers usually cause allocation on the heap, which creates garbage that the garbage collector will have to clean up. If you create too much garbage you will unnecessarily slow down your code.

Sebastian
T in this case is an copy of an type. Different go...

It depends. maps and slices are passed by reference. In case the T is a slice or a map, you are essentially passing a pointer to a reference.

A​kshit
It depends. maps and slices are passed by referenc...

sometimes this is necessary if you want to append onto a slice for example

Roman Sharkov
sometimes this is necessary if you want to append ...

You mean a pointer to a slice? Never seen that.

Both of them produce the same result, but from what i've seen, returning the new slice is "more idiomatic"

Mehrad- Автор вопроса
A​kshit
You mean a pointer to a slice? Never seen that.

append has a new allocation inside (when resizing) if you dont pass pointer you will get wrong behavior

Mehrad
append has a new allocation inside (when resizing)...

It's not "wrong behaviour". Its the behaviour you wrote. The computer does what it is being told.

Thomas
pointer usage is reasonable to me

Its reasonable, and fine to do. But what I'm saying is I have seen more returning the new slice than passing by pointer.

Mehrad- Автор вопроса
A​kshit
It's not "wrong behaviour". Its the behaviour you ...

so u should use pointer not get your wrong behavior

Thomas
ahh yea

Although I'm curious if one is "more optimized" by the compiler than the other.

A​kshit
Although I'm curious if one is "more optimized" by...

The pointer one is likely superior if anything

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта