By Andy Sullivan

BATON ROUGE, La. (Reuters) - President Barack Obama has told law enforcement officials that Americans recognize, respect and depend upon the difficult and dangerous work they do, a rallying call of support following the ambush killings of eight officers in Dallas and Baton Rouge.

Three police officers were gunned down in Louisiana's state capital on Sunday by a U.S. Marine Corps veteran with ties to an African-American anti-government group, authorities said. On July 7, another former U.S. serviceman espousing militant black nationalist views killed five Dallas officers.

"Just as your tight-knit law enforcement family feels the recent losses to your core, our nation grieves alongside you," Obama said in an open letter dated July 18 that was published by the White House on Tuesday.

"Thank you for your courageous service. We have your backs," the president wrote.

Authorities identified the Baton Rouge gunman as former Sergeant Gavin Long of Kansas City, Missouri, an Iraq war veteran, and said he seemed determined to slay as many police officers as possible before a SWAT team marksman cut short his attack.

The single gunshot that killed Long, 29, was fired by an officer from about 100 yards away, police have said as they deepened their investigation into the second racially charged armed assault on U.S. law enforcement this month.

The Dallas shooting happened at the end of an otherwise peaceful protest denouncing the fatal police shootings of two black men days earlier, one of them in Baton Rouge.

Obama said nothing could be more patriotic and professional than police officers protecting demonstrators who were protesting against them, and he said that was a proud example of the country's most basic freedoms.

"This is a time to reaffirm that what makes us special is that we are not only a country, but also a community," he wrote. "That is true whether you are black or white, whether you are rich or poor, whether you are a police officer or someone they protect and serve."

'CALCULATED ACT'

Police have declined to say what role race might have played in Sunday's rampage, which killed two white officers and one black officer. Three more officers were wounded, one of them critically.

But Long, who was black, said in a series of social media messages posted in recent days, some from Dallas, that he was fed up with the mistreatment of African-Americans at the hands of law enforcement, and praised the attack on Dallas police.

Legal papers filed in his home state of Missouri showed he was affiliated with Washitaw Nation, a black offshoot of the Sovereign Citizen movement, which challenges the legitimacy of the federal government.

Baton Rouge police said they believed that Long, armed with two rifles and a pistol, intended to go to their department's headquarters a short distance away to take more lives.

Louisiana State Police Superintendent Colonel Mike Edmonson said there was no doubt that the dead and wounded officers were intentionally targeted and assassinated.

"It was a calculated act against those who work to protect this community every single day," Edmonson told reporters.

The carnage in Baton Rouge rocked a city still shaken by protests over the fatal police shooting on July 5 of 37-year-old black man, Alton Sterling, who was confronted by officers while selling CDs outside a convenience store. Sterling was buried just last Friday.

A day after his killing, another black man, Philando Castile, 32, was shot to death by a policeman during a traffic stop near St. Paul, Minnesota.

The dead officers in Baton Rouge were named as Matthew Gerald, 41, also an Iraq war veteran and father of two; Montrell Jackson, 32, who was black and had served as a Baton Rouge police officer for a decade; and sheriff's deputy Brad Garafola, 45, a father of four.

Hundreds of mourners held a candlelight vigil on Monday evening at a church in south Baton Rouge in memory of Gerald, a rookie on the police force who had served in both the U.S. Army and the Marines.

(Reporting by Andy Sullivan and Sam Karlin in Baton Rouge; Writing by Daniel Wallis; Editing by Bill Trott and Grant McCool)