I usually produce a Granary very early in the build sequence of my first city. This isn't necessarily the best approach. I doubt there is a single best approach.

But this way works well for me.
The timing of the Granary build is worth micro-managing. The Granary's benefit begins the first time the city grows after it has been built. So it is ideal when you can finish the Granary one or two turns before the city will grow. If you finish the Granary say 6 turns before the next growth, you've wasted scouting time. You could have built another Scout before the Granary, still finished the Granary before growing, and had an additional 5 turns of exploring done.
If it is possible to build the Granary before a growth by
delaying growth sometimes I even do that. The GOTM7 start position provides a theoretical example of this. (Theoretical because who knows? You might be able to do better than this after seeing more of the map.) Suppose that:
* We have the GOTM7 start position.
* We settle in the initial position.
* We will only have Salamanca's citizens work on those tiles which are visible at the start.
With those constraints, here's what I would do, doing a deliberate growth slowdown to get the Granary finished at a nice time and to grow faster in the end:
Turn 1: Settle, move the worker to one of the grassland+shield tiles. Start Salamanca producing a Scout. Have Salamanca's citizen work the grassland we're mining.
Turn 2: Start mining the grassland.
Turn 6: Start Salamanca on Granary.
Turn 8: Move Worker toward the other grassland+shield.
Turn 10: Start mining the second grassland.
Turn 11: Set Salamanca's two citizens to work the mined grassland tile and the new grassland tile our Worker is mining. Increase luxury spending to keep citizens happy.
Turn 16: Both grasslands are mined. The Worker doesn't matter anymore for this note. Salamanca will now grow in 5 turns and will finish Granary in 5 turns. This is a problem - the Granary will be one turn too late to help soon and will end up not helping until turn 31. So, switch a citizen in Salamanca from the newly mined grassland to the forest tile, to slow Salamanca's growth but keep production at max.
Turn 18: Switch the citizen working the forest tile back to the mined grassland. Now Salamanca will grow 4 turns from this point.
Turn 21: Salamanca produces Granary. Switch it to producing Settler.
Turn 22: Salamanca grows. It will finish Settler in 4 turns and will grow again in 5 turns. Increase luxury spending until the Settler has been produced.
The final result of the above sequence is that Salamanca will grow four times (enough for two settlers) by turn 32. If we had not built a Granary, it would be turn 41 before growing that far.
I think most people don't produce a Granary this early. It does have some drawbacks. The first Settler is delayed until turn 26 instead of turn 21. We don't get as many Scouts out early. And we start paying 1gold/turn for Granary maintenance. Nonetheless I personally like it because of its fast buildout.
In towns after the capital I rarely build Granaries. It depends on the map and how many Settlers/Workers seem necessary. Usually just a Granary in the capital and perhaps one or two other towns later on where it seems useful.
All this of course depends on the start position, what bonus tiles are available, whether you meet other Civs early on, and so forth. Given a nice start with a Cattle or Game bonus it is possible to get a Warrior or Scout and a Granary before turn 21 without a growth slowdown. In a very food-poor start position I may even poprush a Granary to get it earlier. That may seem counter-intuitive, wasting population to gain growth. I think it makes sense though - about 10 turns after using a citizen to rush Granary I have it back, due to having grown twice instead of just once. 10 turns after that I'm ahead of the game in terms of growth. If food is the major constraint in a start position, this can be a winner. I did this in GOTM5 I think. (Rushed Granary early on.)
Regarding Barracks: I generally build them in most of my core cities, but the timing very much depends on the map. Expansion (Settlers) and exploration come first. Then a Temple or two. Usually Barracks come around that point. Sometimes I'll squeeze in a Barracks before a Settler during the initial expansion phase, if that seems a better use of the shields the town produces while waiting to grow than using those shields on more Warriors/Scouts. It can delay the first Settler from those towns by a few turns but sometimes seems a good compromise.