PHOENIX -- After watching Paul Goldschmidt go hitless in his first three at-bats, Arizona manager Kirk Gibson figured that his first baseman was due.
Gibson was right.
Goldschmidt hit a three-run homer with two outs in the eighth inning and the Diamondbacks rallied to beat the San Francisco Giants 3-1 on Friday night.
"He's pretty special. He really is," Gibson said. "When you've got guys on in front of him, it's hard to just put guys on. We had them in a predicament. They had to pitch him one way or the other."
Giants starter Matt Cain pitched into the eighth, departing after A.J. Pollock beat out a grounder to third for an infield single. Jeremy Affeldt retired pinch-hitter Wil Nieves on a grounder to second and struck out Gerardo Parra looking.
But Affeldt (1-2) walked Willie Bloomquist before Goldschmidt lined a 2-0 pitch into the right-field stands for his team-leading 15th home run of the season.
"It was a dumb pitch. Dumb location. Dumb selection. Stupid," Affeldt said. "For a strike pitch, it wasn't that bad, but that's not what I'm trying to do with him. He is too hot of a hitter, and when you make dumb pitches and you throw to a dumb location, they're going to make you look bad."
Goldschmidt leads the National League with 57 RBIs and has driven in a run in seven straight games, the longest streak by an Arizona player since Chad Tracy in 2005.
In his first three at-bats, Goldschmidt hit into two double plays and struck out.
"There are plenty of times when you don't come through," Goldschmidt said. "You just try to not let those previous at-bats affect your frame of mind."
Brad Ziegler (3-1) got the final two outs of the eighth for the victory. Heath Bell pitched a one-hit ninth for his 11th save.
Arizona starter Patrick Corbin took a no-decision in his bid to become the first Diamondbacks pitcher to start a season 10-0, allowing a run and seven hits and striking out four in 7 1/3 innings.
"There's certainly a good mojo when he's out there," Gibson said. "He's certainly confident when he's out there and he handles situations well."
Cain gave up one run and four hits, struck out four and walked three in seven innings. It was only the third time in 13 starts that Cain had allowed one run or fewer.
"Cain was outstanding," Giants manager Bruce Bochy said. "Our guy matched up well and did his job. We just couldn't finish it."
The Giants broke a scoreless tie in the seventh.
Pablo Sandoval and Brandon Belt started the inning with consecutive singles and Sandoval advanced to third on a fly ball to right, just beating the throw from Parra. Gregor Blanco then lined a single up the middle, scoring Sandoval for the 1-0 lead.
Corbin, who beat the Giants on April 13 with two-hit pitching in 7 1/3 innings, rarely was threatened in the first six innings.
Marco Scutaro singled in the first, Sandoval led off the second with a single and Andres Torres doubled in the third, but all were stranded where they started.
"I kinda felt bad there, losing the lead, but Goldy came through." Corbin said. "I'm just trying to go out there every start and battle. Going up against Cain or whoever, I always feel like it's going to be a close game, so I want to put up as many zeroes as I can."
Cain struggled early, giving up a single to Parra to lead off the game and walking three hitters. But Cain used a double-play grounder by Goldschmidt to escape without allowing a run.
The right-hander appeared upset with home-plate umpire Alan Porter's strike zone and yelled back at him as he left the field.
"I felt fine in the first inning even though I walked three guys," Cain said. "I wasn't all over the place. Some of the pitches weren't that far off. Then I kind of made a little bit of adjustment getting a little more in the strike zone. We just tried to put the first inning behind us after that was over and go from there."
Cain retired nine of the next 10 he faced before Didi Gregorius doubled into the left-field corner leading off the fifth. He then hustled to third on a fly ball to center. Corbin followed with a soft bouncer to first, and Belt easily threw out Gregorius at home. Parra struck out to preserve the scoreless tie.
NOTES: Cain entered the game 13-6 with a 3.47 ERA in 29 career games against the Diamondbacks. ... Affeldt is 1-5 at Chase Field. ... Before the game, the Giants placed OF Angel Pagan on the 15-day disabled list with a strained left hamstring and bursitis. They recalled OF Juan Perez from Triple-A. ... First base umpire Jerry Layne left the game in the bottom of the third inning in the middle of an at-bat by Parra. Layne walked first to the Giants' first-base dugout before exiting down the tunnel to the umpires' dressing room. ... Torres' third-inning double was only his eight hit in 51 at-bats on the road this season.